点击按键拖动并重新排序

小的渣渣,想了很久都没有想明白是不是还有其他办法可以让它自适应,最后只能用boost rap来把这个完成。我一直没有想到拖动是不是有更好的方法,就记得在懒人之家看过这个,所以就直接写下来了。算是给自己的一个学习的例子吧。如果有遇到更好的,我想努力改进!加油!

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任意拖动排序</title>
    <link rel="stylesheet" type="text/css" href="boot/css/bootstrap.css" />
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .item_content {
            width:100%;
            height:100%;
        }
        .item_content .box{
            width:200px;
            height:50px;
            float:left;
            margin:10px
        }
        .item_content .item {
            width:200px;
            height:50px;
            line-height:50px;
            text-align:center;
            cursor:pointer;
            background:#ccc;
            border-radius: 25px;
        }
    </style>
</head>
<body>
<div class="item_content">
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">122</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">123</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">122</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">123</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">122</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">123</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">122</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">123</div>
    </div>
    <div class="box col-lg-3 col-md-4 col-sm-6 col-sx-12">
        <div class="item">111</div>
    </div>
</div>
<script src="jquery.min.js"></script>
<script src="boot/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    $(function() {
        //  1.鼠标位置
        function Pointer(x, y) {
            this.x = x ;
            this.y = y ;
        }
        //  2.相对位置
        function Position(left, top) {
            this.left = left ;
            this.top = top ;
        }
        //  3.要拖动div的相应操作
        $(".item_content .item").each(function(i) {
            //  3.1 初始化
            this.init = function() {
                this.box = $(this).parent() ;//要拖动相应div的父元素
                $(this).attr("index", i).css({
                    position : "absolute",
                    left : this.box.offset().left,
                    top : this.box.offset().top
                }).appendTo(".item_content") ;
                this.drag() ;//拖动
            },
            //  3.2 移动
            this.move = function(callback) {
                $(this).stop(true).animate({
                    left : this.box.offset().left,
                    top : this.box.offset().top
                }, 500, function() {
                    if(callback) {
                        callback.call(this) ;
                    }
                }) ;
            },
            //  3.3 不是需要移动的位置则返回到原来的位置上
            this.collisionCheck = function() {
                var currentItem = this ;    //当前的位置
                var direction = null ;      //移动的状态
                $(this).siblings(".item").each(function() {
                    if(
                        currentItem.pointer.x > this.box.offset().left &&
                        currentItem.pointer.y > this.box.offset().top &&
                        (currentItem.pointer.x < this.box.offset().left + this.box.width()) &&
                        (currentItem.pointer.y < this.box.offset().top + this.box.height())
                    ) {
                        // 返回对象和方向
                        if(currentItem.box.offset().top < this.box.offset().top) {
                            direction = "down" ;
                        } else if(currentItem.box.offset().top > this.box.offset().top) {
                            direction = "up" ;
                        } else {
                            direction = "normal" ;
                        }
                        this.swap(currentItem, direction) ;//需要移动则交换位置
                    }
                }) ;
            },
            //    后者div与原来的div交换位置
            this.swap = function(currentItem, direction) {
                var directions = {
                    normal : function() {
                        //记住原始状态
                        var saveBox = this.box ;
                        this.box = currentItem.box ;
                        currentItem.box = saveBox ;
                        this.move() ;
                        $(this).attr("index", this.box.index()) ;
                        $(currentItem).attr("index", currentItem.box.index()) ;
                    },
                    down : function() {
                        var box = this.box ;
                        var node = this ;
                        var startIndex = currentItem.box.index() ;//初始的序号
                        var endIndex = node.box.index();//最后的序号

                        /*如果是将div往后移动,那么前面的div往前移动一个位置*/
                        for(var i = endIndex; i > startIndex ; i--) {
                            var prevNode = $(".item_content .item[index="+ (i - 1) +"]")[0] ;
                            node.box = prevNode.box ;
                            $(node).attr("index", node.box.index()) ;
                            node.move() ;
                            node = prevNode ;
                        }
                        currentItem.box = box ;
                        $(currentItem).attr("index", box.index()) ;
                    },
                    up : function() {

                        var box = this.box ;
                        var node = this ;
                        var startIndex = node.box.index() ;
                        var endIndex = currentItem.box.index();

                        /*如果是将div往前移动,那么后面的div往后移动一个位置*/
                        for(var i = startIndex; i < endIndex; i++) {
                            var nextNode = $(".item_content .item[index="+ (i + 1) +"]")[0] ;
                            node.box = nextNode.box ;
                            $(node).attr("index", node.box.index()) ;
                            node.move() ;
                            node = nextNode ;
                        }
                        currentItem.box = box ;
                        $(currentItem).attr("index", box.index()) ;
                    }
                };
                directions[direction].call(this) ;
            },
            this.drag = function() { // 拖拽
                var oldPosition = new Position() ;
                var oldPointer = new Pointer() ;
                var isDrag = false ;
                var currentItem = null ;
                $(this).mousedown(function(e) {
                    oldPosition.left = $(this).position().left ;
                    oldPosition.top = $(this).position().top ;
                    oldPointer.x = e.clientX ;
                    oldPointer.y = e.clientY ;
                    isDrag = true ; //鼠标跟随
                    currentItem = this ;
                }) ;
                $(document).mousemove(function(e) {
                    var currentPointer = new Pointer(e.clientX, e.clientY) ;
                    if(!isDrag) return false ;//按下左键
                    $(currentItem).css({
                        "opacity" : "0.8",
                        "z-index" : 999
                    }) ;
                    var left = currentPointer.x - oldPointer.x + oldPosition.left ;
                    var top = currentPointer.y - oldPointer.y + oldPosition.top ;
                    $(currentItem).css({
                        left : left,
                        top : top
                    }) ;
                    currentItem.pointer = currentPointer ;

                    // 开始交换位置
                    currentItem.collisionCheck() ;


                }) ;
                $(document).mouseup(function() {
                    if(!isDrag) return false ;//松开左键
                    isDrag = false ;
                    currentItem.move(function() {
                        $(this).css({
                            "opacity" : "1",
                            "z-index" : 0
                        }) ;
                    }) ;
                }) ;
            };
            this.init() ;
        }) ;
    }) ;
</script>
</body>
</html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值