JQuery实现图片拖拽缩放特效

编程知识 行业动态 更新时间:2024-06-13 00:21:07

给大家分享一个基于JQuery实现的图片拖拽缩放特效,效果如下:

实现代码如下,欢迎大家复制粘贴。

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>基于JQuery实现的图片拖拽缩放特效</title>
    <style type="text/css">
        #box {
            width: 200px;
            height: 100px;
            cursor: move;
            position: absolute;
            top: 30px;
            left: 30px;
            background-color: #FFF;
            border: 1px solid #CCCCCC;
            -webkit-box-shadow: 10px 10px 25px #ccc;
            -moz-box-shadow: 10px 10px 25px #ccc;
            box-shadow: 10px 10px 25px #ccc;
            background: url('images/1.jpg');
            background-size: 100% 100%;
        }

        #dot {
            width: 8px;
            height: 8px;
            overflow: hidden;
            cursor: se-resize;
            position: absolute;
            right: -3px;
            bottom: -3px;
            opacity: 0.8;
            background-color: #09C;
            border: 1px dashed #fff;
        }
    </style>
</head>



<body>

    <div id="box" style="width: 100px; height: 100px; top: 100px; left: 100px;">
        <div id="dot"></div>
    </div>

    <script src="https://ajax.aspnetcdn/ajax/jQuery/jquery-3.1.1.min.js"></script>

    <script>
        $(function () {
            $(document).mousemove(function (e) {

                // 是否为目标移动
                if (!!this.move) {

                    var position = !document.moveTarget ? {'x': 0,'y': 0} : document.moveTarget.position;

                    var callback = document.callDown || function () {

                        var top = e.pageY - position.y;
                        var left = e.pageX - position.x;

                        $(this.moveTarget).css({
                            'top': top,
                            'left': left
                        });
                    };

                    callback.call(this, e, position);
                };

            }).mouseup(function (e) {

                if (!!this.move) {
                    var callback = document.callUp || function () {};

                    callback.call(this, e);

                    $.extend(this, {
                        'move': false,
                        'moveTarget': null,
                        'callDown': false,
                        'callUp': false
                    });

                }
            });

            var $box = $('#box').mousedown(function (e) {

                var offset = $(this).offset();

                this.position = {
                    'x': e.pageX - offset.left,
                    'y': e.pageY - offset.top
                };

                $.extend(document, {
                    'move': true,
                    'moveTarget': this
                });

            }).on('mousedown', '#dot', function (e) {

                var position = {
                    'w': $box.width(),
                    'h': $box.height(),
                    'x': e.pageX,
                    'y': e.pageY
                };

                $.extend(document, {
                    'move': true,
                    'callDown': function (e) {
                        var width = e.pageX - position.x + position.w;
                        var height = e.pageY - position.y + position.h;
                        $box.css({
                            'width': Math.max(30, width),
                            'height': Math.max(30, height)
                        });
                    }
                });
                return false;
            })
        });
    </script>  

</body>
</html>

更多推荐

JQuery实现图片拖拽缩放特效

本文发布于:2023-03-31 03:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/85f339474739a1b0994d9c4edd616832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩放   拖拽   特效   图片   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!