jquery拖拽排序效果
原文:http://www.cnblogs.com/web-ed2/archive/2011/09/19/2181819.html
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>测试的拖拽功能</title> 6 <style type="text/css"> 7 body, div { margin: 0; paading: 0; font-size: 12px; } 8 body { width: 960px; margin: 0 auto; } 9 ul, li { margin: 0; padding: 0; list-style: none; } 10 .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; } 11 12 .box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } 13 .main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } 14 .maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } 15 16 .hide { width: 600px; height: 80px; margin-bottom: 5px; } 17 .dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; }; 18 </style> 19 <script type="text/javascript" src="jquery-1.6.1.min.js"></script> 20 <script type="text/javascript"> 21 22 $(document).ready( function () { 23 24 var range = { x: 0, y: 0 };//鼠标元素偏移量 25 var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽对象的四个坐标 26 var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目标元素对象的坐标初始化 27 28 var theDiv = null, move = false;//拖拽对象 拖拽状态 29 var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽对象的索引、高度、的初始化。 30 var tarDiv = null, tarFirst, tempDiv; //要插入的目标元素的对象, 临时的虚线对象 31 32 $(".main").each(function(){ 33 $(this).mousedown(function (event){ 34 //拖拽对象 35 theDiv = $(this); 36 37 //鼠标元素相对偏移量 38 range.x = event.pageX - theDiv.offset().left; 39 range.y = event.pageY - theDiv.offset().top; 40 41 theDivId = theDiv.index(); 42 theDivHeight = theDiv.height(); 43 theDivHalf = theDivHeight/2; 44 move = true; 45 46 theDiv.attr("class","maindash"); 47 // 创建新元素 插入拖拽元素之前的位置(虚线框) 48 $("<div class='dash'></div>").insertBefore(theDiv); 49 50 }); 51 }); 52 53 $(document).mousemove(function(event) { 54 55 if (!move) return false; 56 57 lastPos.x = event.pageX - range.x; 58 lastPos.y = event.pageY - range.y; 59 lastPos.y1 = lastPos.y + theDivHeight; 60 61 // 拖拽元素随鼠标移动 62 theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'}); 63 // 拖拽元素随鼠标移动 查找插入目标元素 64 65 var $main = $('.main'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标, 66 tempDiv = $(".dash"); //获得临时 虚线框的对象 67 68 $main.each(function () { 69 tarDiv = $(this); 70 tarPos.x = tarDiv.offset().left; 71 tarPos.y = tarDiv.offset().top; 72 tarPos.y1 = tarPos.y + tarDiv.height()/2; 73 74 tarFirst = $main.eq(0); // 获得第一个元素 75 tarFirstY = tarFirst.offset().top + theDivHalf ; // 第一个元素对象的中心纵坐标 76 77 //拖拽对象 移动到第一个位置 78 if (lastPos.y <= tarFirstY) { 79 tempDiv.insertBefore(tarFirst); 80 } 81 //判断要插入目标元素的 坐标后, 直接插入 82 if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) { 83 tempDiv.insertAfter(tarDiv); 84 } 85 86 }); 87 88 }).mouseup(function(event) { 89 90 theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虚线div的位置上 91 theDiv.attr("class", "main"); //恢复对象的初始样式 92 93 $('.dash').remove(); // 删除新建的虚线div 94 move=false; 95 96 }); 97 98 }); 99 100 101 </script> 102 </head> 103 104 <body> 105 <div class="box" id="box"> 106 <div class="main" id="main0">div1</div> 107 <div class="main" id="main1">div2</div> 108 <div class="main" id="main2">div3</div> 109 <div class="main" id="main3">div4</div> 110 <div class="main" id="main4">div5</div> 111 </div> 112 </body> 113 </html>