---
title: "元素的拖拽与重排"
author: "keimon"
date: "2014-01-04T12:53:53Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2014-01-04T12:57:26Z"
id: 1998946
url: "https://keimon.iteye.com/blog/1998946"
---

&lt;!DOCTYPE html&gt;  
&lt;html&gt;  
&lt;head&gt;  
    &lt;meta charset="utf-8"&gt;  
    &lt;title&gt;&lt;/title&gt;  
    &lt;style type="text/css"&gt;  
        ul, li, div, span, img{margin: 0; padding: 0;}  
        #mydiv  
        {  
            width: 900px;  
            background-color: #fff;  
            border: 1px solid #000;  
            position: absolute;  
            left: 100px;  
            height: 500px;  
            overflow: scroll;  
        }  
        #mydiv ul{position: relative; overflow: hidden; list-style: none; margin-top: 10px; margin-left: 10px;}  
        #mydiv li{float: left; background-color: #e1e1e1; margin-left: 10px; margin-bottom: 10px; width: 200px; height: 226px; position: relative;}  
        #mydiv span{display: block; padding: 4px 10px;}  
        img{width: 200px; height: 200px; background-color: #999;}

        .moveEle{position: absolute; z-index: 2; background-color: #e1e1e1; width: 200px; height: 226px; left: 0; right: 0;}  
    &lt;/style&gt;  
    &lt;script src="../../jquery-1.7.2.js" type="text/javascript"&gt;&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div id="mydiv"&gt;  
    &lt;ul&gt;  
        &lt;li class=""&gt;  
            &lt;span&gt;1&lt;/span&gt;  
            &lt;img src="images/img1.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;2&lt;/span&gt;  
            &lt;img src="images/img2.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;3&lt;/span&gt;  
            &lt;img src="images/img3.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;4&lt;/span&gt;  
            &lt;img src="images/img4.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;5&lt;/span&gt;  
            &lt;img src="images/img5.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;6&lt;/span&gt;  
            &lt;img src="images/img6.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;7&lt;/span&gt;  
            &lt;img src="images/img7.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;8&lt;/span&gt;  
            &lt;img src="images/img8.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;9&lt;/span&gt;  
            &lt;img src="images/img9.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;10&lt;/span&gt;  
            &lt;img src="images/img10.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;11&lt;/span&gt;  
            &lt;img src="images/img11.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
        &lt;li&gt;  
            &lt;span&gt;12&lt;/span&gt;  
            &lt;img src="images/img12.jpg" width="200" height="200"/&gt;  
        &lt;/li&gt;  
    &lt;/ul&gt;  
&lt;/div&gt;  
&lt;script src="../../jquery-1.7.2.js"&gt;&lt;/script&gt;  
&lt;script type="text/javascript"&gt;  
    /\**  
     \*元素移动效果  
     * made by keimon  
     \*/  
    $(function () {  
        var \_bool = false, //鼠标是否按下  
             cloneEle,  
             moveEle,  
             evtX,  
             evtY;  
        $('ul li').on('mousedown', function (e) {  
            \_bool = true;  
            cloneEle = $(this).clone(true);  
            moveEle = "&lt;div class='moveEle'&gt;"+cloneEle.html()+"&lt;/div&gt;"; //移动的新元素  
            $('body').append(moveEle);  
            $(this).html("").addClass('emptyEle'); //空元素  
            evtX = e.pageX;  
            evtY = e.pageY;  
            $('.moveEle').css('left', evtX + 'px');  
            $('.moveEle').css('top', evtY + 'px');  
        });  
        $(document).delegate('li','mousemove', function (e) {  
            if (!\_bool) {  
                return false  
            }  
            evtX = e.pageX;  
            evtY = e.pageY;  
            $('.moveEle').css('left', evtX + 'px');  
            $('.moveEle').css('top', evtY + 'px');  
            if($(e.currentTarget).hasClass('emptyEle')){  
                return false;  
            }else{  
                var curIndex = $('ul li').index($(e.currentTarget));  
                var empIndex = $('ul li').index($('.emptyEle'));  
                if(curIndex&lt;empIndex){  
                    $(e.currentTarget).before($('.emptyEle'));  
                }else{  
                    $(e.currentTarget).after($('.emptyEle'));  
                }  
            }  
        });  
        $(document).bind('mouseup', function () {  
            if (\_bool) {  
                cloneEle.css({'left':'auto', 'top':'auto'});  
                $('.emptyEle').replaceWith(cloneEle);  
                $('.moveEle').remove();  
                \_bool = false;  
            }  
        })  
    })  
&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;