日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

html实现拖拽排序,简单的jquery拖拽排序效果实现代码

發(fā)布時間:2023/12/1 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html实现拖拽排序,简单的jquery拖拽排序效果实现代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

步驟:

1.實(shí)現(xiàn)隨鼠標(biāo)移動的效果;

2.初始化一個元素及其坐標(biāo);

3.拖拽對象的最后坐標(biāo),與元素的坐標(biāo) 進(jìn)行計算和判斷 來確定 要插入的目標(biāo)元素;

4.用insertBefore 方法 插入到目標(biāo)元素的前面

具體代碼如下:

測試的拖拽功能

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; };

$(document).ready( function () {

var range = { x: 0, y: 0 };//鼠標(biāo)元素偏移量

var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽對象的四個坐標(biāo)

var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目標(biāo)元素對象的坐標(biāo)初始化

var theDiv = null, move = false;//拖拽對象 拖拽狀態(tài)

var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽對象的索引、高度、的初始化。

var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對象, 臨時的虛線對象

$(".main").each(function(){

$(this).mousedown(function (event){

//拖拽對象

theDiv = $(this);

//鼠標(biāo)元素相對偏移量

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");

// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)

$("

});

});

$(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;

// 拖拽元素隨鼠標(biāo)移動

theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});

// 拖拽元素隨鼠標(biāo)移動 查找插入目標(biāo)元素

var $main = $('.main'); // 局部變量:按照重新排列過的順序 再次獲取 各個元素的坐標(biāo),

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 ; // 第一個元素對象的中心縱坐標(biāo)

//拖拽對象 移動到第一個位置

if (lastPos.y <= tarFirstY) {

tempDiv.insertBefore(tarFirst);

}

//判斷要插入目標(biāo)元素的 坐標(biāo)后, 直接插入

if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) {

tempDiv.insertAfter(tarDiv);

}

});

}).mouseup(function(event) {

theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上

theDiv.attr("class", "main"); //恢復(fù)對象的初始樣式

tempDiv.remove(); // 刪除新建的虛線div

move=false;

});

});

div1div2div3div4div5

總結(jié)

以上是生活随笔為你收集整理的html实现拖拽排序,简单的jquery拖拽排序效果实现代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。