簡單的jquery拖拽排序效果實現代碼

來源:互聯網
上載者:User

步驟:
1.實現隨滑鼠移動的效果;
2.初始化一個元素及其座標;
3.拖拽對象的最後座標,與元素的座標 進行計算和判斷 來確定 要插入的目標元素;
4.用insertBefore 方法 插入到目標元素的前面
具體代碼如下:
複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測試的拖拽功能</title>
<style type="text/css">
body, div { margin: 0; paading: 0; font-size: 12px; }
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; }
.hide { width: 600px; height: 80px; margin-bottom: 5px; }
.dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; };
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
var range = { x: 0, y: 0 };//滑鼠元素位移量
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 theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽對象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv; //要插入的目標元素的對象, 臨時的虛線對象
$(".main").each(function(){
$(this).mousedown(function (event){
//拖拽對象
theDiv = $(this);
//滑鼠元素相對位移量
range.x = event.pageX - theDiv.offset().left;
range.y = event.pageY - theDiv.offset().top;
theDivId = theDiv.index();
theDivHeight = theDiv.height();
theDivHalf = theDivHeight/2;
move = true;
theDiv.attr("class","maindash");
// 建立新元素 插入拖拽元素之前的位置(虛線框)
$("<div class='dash'></div>").insertBefore(theDiv);
});
});
$(document).mousemove(function(event) {
if (!move) return false;
lastPos.x = event.pageX - range.x;
lastPos.y = event.pageY - range.y;
lastPos.y1 = lastPos.y + theDivHeight;
// 拖拽元素隨滑鼠移動
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素隨滑鼠移動 尋找插入目標元素
var $main = $('.main'); // 局部變數:按照重新排列過的順序 再次擷取 各個元素的座標,
tempDiv = $(".dash"); //獲得臨時 虛線框的對象
$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 + theDivHalf ; // 第一個元素對象的中心縱座標
//拖拽對象 移動到第一個位置
if (lastPos.y <= tarFirstY) {
tempDiv.insertBefore(tarFirst);
}
//判斷要插入目標元素的 座標後, 直接插入
if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) {
tempDiv.insertAfter(tarDiv);
}
});
}).mouseup(function(event) {
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
theDiv.attr("class", "main"); //恢複對象的初始樣式
tempDiv.remove(); // 刪除建立的虛線div
move=false;
});
});
</script>
</head>
<body>
<div class="box" id="box">
<div class="main" id="main0">div1</div>
<div class="main" id="main1">div2</div>
<div class="main" id="main2">div3</div>
<div class="main" id="main3">div4</div>
<div class="main" id="main4">div5</div>
</div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.