jQuery简单实现div层的放大与缩小

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .content
        {
            border: 1px solid #ccc;
            width: 440px;
            overflow: hidden;
            margin: 10px;
        }
    </style>

    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            if ($('#content').height() > 400)
                $('#content').height(400);
            $('#bigger').toggle(function() {
                $('#content').height($('#content').height() + 100);
                $('#bigger').html('缩小');
            }, function() {
                $('#content').height($('#content').height() - 100);
                $('#bigger').html('放大');
            })
        });
    </script>

</head>
<body>
    <div id="content" class="content">
        內容1<br />
        內容2<br />
        內容3<br />
        內容4<br />
        內容5<br />
        內容6<br />
        內容7<br />
        內容8<br />
        內容9<br />
        內容10<br />
        內容11<br />
        內容12<br />
        內容13<br />
        內容14<br />
        內容15<br />
        內容16<br />
        內容17<br />
        內容18<br />
        內容19<br />
        內容20<br />
        內容21<br />
        內容22<br />
        內容23<br />
        內容24<br />
        內容25<br />
    </div>
    <br />
    <span id="bigger">放大</span>
</body>
</html>

转载于:https://www.cnblogs.com/gdjlc/archive/2010/07/08/2086893.html

使用jQuery原生可以很容易实现可拖拽放大缩小的弹出框。首先,需要引入jQuery库,在HTML中创建弹出框的结构,并设置好初始样式。然后使用jQuery的事件绑定方法,为弹出框的标题栏添加鼠标按下事件,实现拖拽功能。接着,为弹出框的放大缩小按钮添加点击事件,通过改变弹出框的宽高和位置实现放大缩小效果。具体实现步骤如下: 1. 引入jQuery库: ```html <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> ``` 2. 创建弹出框结构: ```html <div class="popup"> <div class="popup-title">弹出框标题</div> <div class="popup-content"> <!-- 弹出框内容 --> </div> <div class="popup-controls"> <button class="btn-zoom-in">放大</button> <button class="btn-zoom-out">缩小</button> </div> </div> ``` 3. 设置初始样式: ```css .popup { position: absolute; top: 50px; left: 50px; width: 200px; height: 150px; border: 1px solid #000; background-color: #fff; } .popup-title { cursor: move; } ``` 4. 实现拖拽功能: ```javascript $('.popup-title').on('mousedown', function(e) { var offsetX = e.clientX - $(this).offset().left; var offsetY = e.clientY - $(this).offset().top; $(document).on('mousemove', function(e) { $('.popup').offset({ top: e.clientY - offsetY, left: e.clientX - offsetX }); }); $(document).on('mouseup', function() { $(document).off('mousemove'); $(document).off('mouseup'); }); }); ``` 5. 实现放大缩小功能: ```javascript $('.btn-zoom-in').on('click', function() { var width = $('.popup').width(); var height = $('.popup').height(); $('.popup').css({ width: width * 1.2, height: height * 1.2 }); }); $('.btn-zoom-out').on('click', function() { var width = $('.popup').width(); var height = $('.popup').height(); $('.popup').css({ width: width * 0.8, height: height * 0.8 }); }); ``` 通过以上步骤,就可以实现一个带拖拽、放大缩小功能的弹出框。当用户鼠标点击弹出框标题栏后,即可拖动整个弹出框;点击放大缩小按钮后,即可实现弹出框的放大缩小效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值