脚本宝典收集整理的这篇文章主要介绍了js實例教程-jQuery拖拽排序的實現教程,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小寶典致力於為廣大程序猿(媛)提供高品質的代碼服務,請大家多多光顧小站,小寶典在此謝過。
修改了原文章一個致命bug,以及刪除了認為多餘的代碼,下一步就是實現數據異步提交了
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <tITle>jquery拖拽</title> <style type="text/css"> body, p { margin: 0; paading: 0; font-Size: 12px;user-select: none;} body { width: 960px; margin: 0 auto; } ul, li { margin: 0; padding: 0; list-style: none; } .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; } .box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } .main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } .maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } .dash { width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; display:block;}; </style> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(document).ready( function () { VAR lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽對象的四個坐標 var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目標元素對象的坐標初始化 var theDiv = null, move = false;//拖拽對象 拖拽狀態 var theDivHeight = 0, tarFirstY = 0; //拖拽對象的索引、高度、的初始化。 var tarDiv = null, tarFirst = null; //要插入的目標元素的對象, 臨時的虛線對象 var tempDiv = $("<p class='dash'></p>"); //虛線框對象 $(".main").mousedown(function (event){ //拖拽對象 theDiv = $(this); theDivHeight = theDiv.height(); move = true; theDiv.attr("class","maindash"); theDiv.css({top: event.pageY-theDivHeight/2}); tempDiv.insertBefore(theDiv); }); $(document).mouSEMove(function(event) { if (!move) return false; lastPos.y = event.pageY - theDivHeight/2; lastPos.y1 = lastPos.y + theDivHeight; // 拖拽元素隨鼠標移動 theDiv.css({top: lastPos.y}); // 拖拽元素隨鼠標移動 查找插入目標元素 var $main = $('.main'); // 局部變量:按照重新排列過的順序 再次獲取 各個元素的坐標, $main.each(function () { tarDiv = $(this); tarPos.x = tarDiv.offset().left; tarPos.y = tarDiv.offset().top; tarPos.y1 = tarPos.y + tarDiv.height()/2; tarFirst = $main.eq(0); // 獲得第一個元素 tarFirstY = tarFirst.offset().top + theDivHeight/2 ; // 第一個元素對象的中心縱坐標 //拖拽對象 移動到第一個位置 if (lastPos.y <= tarFirstY) { tempDiv.insertBefore(tarFirst); } //判斷要插入目標元素的 坐標后, 直接插入 if (lastPos.y >= tarPos.y - theDivHeight/2 && lastPos.y1 >= tarPos.y1 ) { tempDiv.insertAfter(tarDiv); } }); }).mouseup(function(event) { theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線p的位置上 theDiv.attr("class", "main"); //恢復對象的初始樣式 theDiv =null; //用完的對象記得釋放 tempDiv.remove(); // 刪除新建的虛線p move=false; }); }); </script> </head> <body> <p class="box" id="box"> <p class="main" id="main0">p1</p> <p class="main" id="main1">p2</p> <p class="main" id="main2">P3</p> <p class="main" id="main3">p4</p> <p class="main" id="main4">p5</p> </p> </body> </html>@H_406_5@
修改了原文章一個致命bug,以及刪除了認為多餘的代碼,下一步就是實現數據異步提交了
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery拖拽</title> <style type="text/css"> body, p { margin: 0; paading: 0; font-size: 12px;user-select: none;} body { width: 960px; margin: 0 auto; } ul, li { margin: 0; padding: 0; list-style: none; } .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; } .box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } .main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } .maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } .dash { width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; display:block;}; </style> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(document).ready( function () { var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽對象的四個坐標 var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目標元素對象的坐標初始化 var theDiv = null, move = false;//拖拽對象 拖拽狀態 var theDivHeight = 0, tarFirstY = 0; //拖拽對象的索引、高度、的初始化。 var tarDiv = null, tarFirst = null; //要插入的目標元素的對象, 臨時的虛線對象 var tempDiv = $("<p class='dash'></p>"); //虛線框對象 $(".main").mousedown(function (event){ //拖拽對象 theDiv = $(this); theDivHeight = theDiv.height(); move = true; theDiv.attr("class","maindash"); theDiv.css({top: event.pageY-theDivHeight/2}); tempDiv.insertBefore(theDiv); }); $(document).mousemove(function(event) { if (!move) return false; lastPos.y = event.pageY - theDivHeight/2; lastPos.y1 = lastPos.y + theDivHeight; // 拖拽元素隨鼠標移動 theDiv.css({top: lastPos.y}); // 拖拽元素隨鼠標移動 查找插入目標元素 var $main = $('.main'); // 局部變量:按照重新排列過的順序 再次獲取 各個元素的坐標, $main.each(function () { tarDiv = $(this); tarPos.x = tarDiv.offset().left; tarPos.y = tarDiv.offset().top; tarPos.y1 = tarPos.y + tarDiv.height()/2; tarFirst = $main.eq(0); // 獲得第一個元素 tarFirstY = tarFirst.offset().top + theDivHeight/2 ; // 第一個元素對象的中心縱坐標 //拖拽對象 移動到第一個位置 if (lastPos.y <= tarFirstY) { tempDiv.insertBefore(tarFirst); } //判斷要插入目標元素的 坐標后, 直接插入 if (lastPos.y >= tarPos.y - theDivHeight/2 && lastPos.y1 >= tarPos.y1 ) { tempDiv.insertAfter(tarDiv); } }); }).mouseup(function(event) { theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線p的位置上 theDiv.attr("class", "main"); //恢復對象的初始樣式 theDiv =null; //用完的對象記得釋放 tempDiv.remove(); // 刪除新建的虛線p move=false; }); }); </script> </head> <body> <p class="box" id="box"> <p class="main" id="main0">p1</p> <p class="main" id="main1">p2</p> <p class="main" id="main2">p3</p> <p class="main" id="main3">p4</p> <p class="main" id="main4">p5</p> </p> </body> </html>
覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!
以上是脚本宝典为你收集整理的js實例教程-jQuery拖拽排序的實現教程全部内容,希望文章能够帮你解决js實例教程-jQuery拖拽排序的實現教程所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。