当前位置:首页 > 技术博客 > JQuery > 基于jQuery的拖拽效果

基于jQuery的拖拽效果

4年前 (2021-07-28)JQuery974

描述:

基于jQuery实现的拖拽插件,简单好用

<head>    
<meta charset="UTF-8">    
<meta name="viewport" content="width=device-width, initial-scale=1.0">    
<title>拖拽插件</title>    
</head>    
<style>    
* {    
list-style: none;    
padding: 0;    
margin: 0;    
}    
#dragMenu {    
position: relative;    
width: 1400px;    
margin: 0 auto;    
}    
#dragMenu::after{    
content: '';    
display: block;    
clear: both;    
}    
li {    
width: 30%;    
height: 250px;    
float: left;    
cursor: move;    
margin: 10px 5px;    
}    
li.active{    
border: 1px solid red;    
}    
img {    
width: 100%;    
height: 100%;    
display: block;    
}    
</style>    
<body>    
<ul id="dragMenu">    
<li><img src="https://www.uxqr.com/background.php?1" /></li>    
<li><img src="https://www.uxqr.com/background.php?2" /></li>    
<li><img src="https://www.uxqr.com/background.php?3" /></li>    
<li><img src="https://www.uxqr.com/background.php?4" /></li>    
<li><img src="https://www.uxqr.com/background.php?5" /></li>    
<li><img src="https://www.uxqr.com/background.php?6" /></li>    
</ul>    
</body>    
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>    
 
<script>    
    
$(function (window, $) {
    DragMenu.prototype.init = function () {
        this.setOffset();
    }

    DragMenu.prototype.setOffset = function () {    // 初始赋值
       var _this = this;
       var lis = this.getLis();
       $.each(lis, function (index, item) {
           var left = item.offsetLeft;
           var top = item.offsetTop;
           $(item).css({
                left: left,
                top: top
           });  
           item.index = index;
           _this.pos.push({left: left, top: top});
           _this.bindEvent(item);
       });
       lis.css({position: 'absolute'});
    }

    DragMenu.prototype.getLis = function () {   // 获取lis元素
        return $(this.container).find('li');
    }

    DragMenu.prototype.getMin = function (node) {   // 求出距离最近的元素
        var _this = this;
        var lis = this.getLis();  
        var index = null;  
        var minDis = Infinity;
        for(var i = 0; i < lis.length; i ++) {
            if(node === lis[i]) {continue;}
            if(this.crashTest(node, lis[i])) {
                var dis = this.calcDistance(node, lis[i]);
                if(dis < minDis) {
                    minDis = dis;
                    index = i;
                }
            }
        }
        return lis[index];
    }

    DragMenu.prototype.crashTest = function (node1, node2) {    // 碰撞检测
        var t1 = node1.offsetTop;
        var r1 = node1.offsetWidth+node1.offsetLeft;
        var b1 = node1.offsetHeight+node1.offsetTop;
        var l1 = node1.offsetLeft;

        var t2 = node2.offsetTop;
        var r2 = node2.offsetWidth+node2.offsetLeft;
        var b2 = node2.offsetHeight+node2.offsetTop;
        var l2 = node2.offsetLeft;

        if(t1>b2||r1<l2||b1<t2||l1>r2){
            return false;
        }else{
            return true;
        }
    }

    DragMenu.prototype.calcDistance = function (node1, node2) { // 计算出距离
        var a = node1.offsetLeft - node2.offsetLeft;
        var b = node1.offsetTop - node2.offsetTop;
        return Math.sqrt(Math.pow(a,2)+ Math.pow(b,2));
    }

    DragMenu.prototype.move = function (node, json) {  // 移动
        var _this = this;
        node.stop().animate(json, this.options.speed, this.options.easing, function () {
            $(node).css({zIndex: 0});
            _this.count --;
            !_this.count && typeof _this.options.animateCallback === 'function' && _this.options.animateCallback();
        });
    }

    DragMenu.prototype.bindEvent = function (node) {    // 事件绑定
        var _this = this;
        node.onmousedown = function (event) {
            var scrollLeft = document.documentElement.scrollLeft||document.body.scrollLeft;
            var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;

            //当鼠标按下时计算鼠标与拖拽对象的距离
            _this.disX = event.clientX + scrollLeft - node.offsetLeft;
            _this.disY = event.clientY + scrollTop - node.offsetTop;
            document.onmousemove = function (event) {   
                var left = event.clientX - _this.disX + scrollLeft;
				var top = event.clientY - _this.disY + scrollTop;
                $(node).css({left: left, top: top, zIndex: 1}); 
                _this.minNode = _this.getMin(node);
                if(_this.minNode) {
                    $(_this.minNode).addClass('active').siblings().removeClass('active');
                }
            }
        
            document.onmouseup = function (event) {
                document.onmousemove = null;
				document.onmouseup = null;
                _this.getLis().removeClass('active');
                var tempIndex = 0;
                if(_this.minNode) {
                    _this.count = 2;
                    _this.move($(node), _this.pos[_this.minNode.index]);
                    _this.move($(_this.minNode), _this.pos[node.index]);
                    tempIndex = _this.minNode.index;
                    _this.minNode.index = node.index;
                    node.index = tempIndex;
                }else {
                    _this.count = 1;
                    _this.move($(node), _this.pos[node.index]);
                }

            }
            _this.getMin(node);
            return false;
        }
    }

    function DragMenu(options) {
        this.options = {
            container: '#container',
            easing: 'swing',
            speed: 500,
            animateCallback: function () {
                
            }
        }
        this.options = $.extend({}, this.options, options);
        this.container = this.options.container;
        this.disX = 0;
        this.disY = 0;
        this.minZIndex = 0;
        this.minNode = null;
        this.pos = [];
    }

    $.fn.extend({
        dragMenu: function (options) {
            options = options || {}
            options["container"] = this;
            new DragMenu(options).init();
        }
    });
    

}(window, jQuery));
$('#dragMenu').dragMenu();
</script>

电脑在线的朋友可以直接点击上面的【运行代码】查看特效


使用说明


属性说明类型默认值 
container需要拖拽功能的容器String#container
easing动画效果Stringswing
speed动画执行完成的时间Number500
animateCallback动画执行完成的回调函数Functionfunctoin() {}


版权声明:本文由 LzxBlog 发布,如需转载请注明出处。

本文链接:https://www.liuzhixi.cn/html/138.html

标签: jQuery拖拽

相关文章

JQuery动态添加元素方法

JQuery动态添加元素方法

JQuery动态添加元素方法append()在父级最后追加一个子元素 appendTo()在父级最后追加一个子元素 prepend()在父级...

jQuery响应式工作室类网站模板

jQuery响应式工作室类网站模板

简洁创意的jQuery响应式工作室类网站模板下载链接:https://pan.baidu.com/s/17qCoDvqTAt98mygA4b4jCQ ...

jQuery动态数据流程步骤条特效

jQuery动态数据流程步骤条特效

jQuery动态数据流程步骤条特效,事先设置好需要走的流程步骤,然后根据状态进行判断走到哪一步,审批意见是什么,支持动态数据配置。<html>&nb...

jQuery html5背景视频插件

jQuery html5背景视频插件

vidbacking是一款响应式的,跨平台的html5视频背景插件。该视频背景插件允许你在页面中的某个div后整个页面中使用HTML5视频作为背景使用方法在页面...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。