日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

步驟:

1.實現隨鼠標移動的效果;

2.初始化一個元素及其坐標;

3.拖拽對象的最后坐標,與元素的坐標 進行計算和判斷 來確定 要插入的目標元素;

4.用insertBefore 方法 插入到目標元素的前面

具體代碼如下:

測試的拖拽功能

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 };//鼠標元素偏移量

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

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

$("

});

});

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

});

});

div1div2div3div4div5

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。