簡(jiǎn)單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)動(dòng)態(tài))
更新時(shí)間:2017年02月09日 14:13:58 作者:bayaci
這篇文章主要介紹了簡(jiǎn)單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)),增強(qiáng)動(dòng)態(tài)增加div效果,代碼簡(jiǎn)單,很容易實(shí)現(xiàn),需要的朋友可以參考下
增強(qiáng)動(dòng)態(tài)增加Div效果

原來(lái)沒有新建動(dòng)作,分析代碼后發(fā)現(xiàn)很容易增強(qiáng)~~
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測(cè)試的拖拽功能</title>
<style type="text/css">
body, div { margin: 0; paading: 0; font-size: 12px; }
body { width:100%; 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; }
.drag_module_box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
.drag_module_box1 { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
.drag_module_main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; }
.drag_module_maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }
.drag_module_hide { width: 600px; height: 80px; margin-bottom: 5px; }
.drag_module_dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; };
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
//來(lái)源:http://www.cnblogs.com/web-ed2/archive/2011/09/19/2181819.html
var range = { x: 0, y: 0 };//鼠標(biāo)元素偏移量
var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽對(duì)象的四個(gè)坐標(biāo)
var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目標(biāo)元素對(duì)象的坐標(biāo)初始化
var theDiv = null, move = false;//拖拽對(duì)象 拖拽狀態(tài)
var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽對(duì)象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對(duì)象, 臨時(shí)的虛線對(duì)象
function loopbox(){ //循環(huán)初始化
$(".drag_module_box").find(".drag_module_main").each(function(){
console.log( 'find' );
$(this).mousedown(function (event){
//拖拽對(duì)象
theDiv = $(this);
//鼠標(biāo)元素相對(duì)偏移量
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","drag_module_maindash");
// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)
$("<div class='drag_module_dash'></div>").insertBefore(theDiv);
});
});
}
loopbox();
$(".drag_module_box").mousemove(function(event) {
console.log( 'mousemove' );
if (!move) return false;
lastPos.x = event.pageX - range.x;
lastPos.y = event.pageY - range.y;
lastPos.y1 = lastPos.y + theDivHeight;
// 拖拽元素隨鼠標(biāo)移動(dòng)
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素隨鼠標(biāo)移動(dòng) 查找插入目標(biāo)元素
var $main = $('.drag_module_main'); // 局部變量:按照重新排列過的順序 再次獲取 各個(gè)元素的坐標(biāo),
tempDiv = $(".drag_module_dash"); //獲得臨時(shí) 虛線框的對(duì)象
$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); // 獲得第一個(gè)元素
tarFirstY = tarFirst.offset().top + theDivHalf ; // 第一個(gè)元素對(duì)象的中心縱坐標(biāo)
//拖拽對(duì)象 移動(dòng)到第一個(gè)位置
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) {
console.log( 'mouseup' );
if(theDiv==null) return false;
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
theDiv.attr("class", "drag_module_main"); //恢復(fù)對(duì)象的初始樣式
$('.drag_module_dash').remove(); // 刪除新建的虛線div
move=false;
});
$("#drag_module_insert").click(function(){
$("#drag_module_box1").html($("#drag_module_box1").html()+$("#drag_module_box2").html());
loopbox();
});
$("#drag_module_seque").click(function(){
$(".drag_module_box").find(".drag_module_main").each(function(){
console.log($(this).attr('id'));
});
});
});
</script>
</head>
<body>
<div class="drag_module_box" id="drag_module_box1">
<div class="drag_module_main" id="main1">div1</div>
<div class="drag_module_main" id="main2">div2</div>
<div class="drag_module_main" id="main3">div3</div>
<div class="drag_module_main" id="main4">div4</div>
<div class="drag_module_main" id="main5">div5</div>
<div class="drag_module_main" id="main6">div6</div>
</div>
<div class="drag_module_box1" id="drag_module_box2">
<div class="drag_module_main" id="main_first">div7</div>
</div>
<input type="button" value="新建" id="drag_module_insert"/>
<input type="button" value="確定" id="drag_module_seque"/>
</body>
</html>
您可能感興趣的文章:
- 針對(duì)后臺(tái)列表table拖拽比較實(shí)用的jquery拖動(dòng)排序
- jQuery實(shí)現(xiàn)div橫向拖拽排序的簡(jiǎn)單實(shí)例
- 通過jquery-ui中的sortable來(lái)實(shí)現(xiàn)拖拽排序的簡(jiǎn)單實(shí)例
- jQuery拖拽排序插件制作拖拽排序效果(附源碼下載)
- jquery拖拽排序簡(jiǎn)單實(shí)現(xiàn)方法(效果增強(qiáng)版)
- jqueryUI里拖拽排序示例分析
- 簡(jiǎn)單的jquery拖拽排序效果實(shí)現(xiàn)代碼
- jquery拖拽自動(dòng)排序插件使用方法詳解
相關(guān)文章
JavaScript實(shí)現(xiàn)的滾動(dòng)公告特效【基于jQuery】
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的滾動(dòng)公告特效,結(jié)合完整實(shí)例形式詳細(xì)分析了基于jQuery實(shí)現(xiàn)的頁(yè)面元素間歇修改,最終達(dá)到滾動(dòng)公告效果的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
jQuery EasyUI API 中文文檔 - Tree樹使用介紹
jQuery EasyUI API 中文文檔 - Tree樹使用介紹,需要的朋友可以參考下。2011-11-11
JQuery調(diào)用WebServices的方法和4個(gè)實(shí)例
你是不是經(jīng)常作這種開發(fā),前端用JS寫邏輯,后端用aspx或者ashx作服務(wù)?你是不是經(jīng)常在請(qǐng)求aspx的時(shí)候在查詢字符串中拼接諸如a.aspx?method=getDepartmetn¶m1=1¶m2=2的字符串?2014-05-05
jQuery實(shí)現(xiàn)頁(yè)面頂部顯示的進(jìn)度條效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)頁(yè)面頂部顯示的進(jìn)度條效果,以完整實(shí)例形式分析了jQuery基于animate與setTimeout定時(shí)觸發(fā)實(shí)現(xiàn)進(jìn)度條漸進(jìn)顯示功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12
jquery實(shí)現(xiàn)的仿天貓側(cè)導(dǎo)航tab切換效果
這篇文章主要介紹了jquery實(shí)現(xiàn)的仿天貓側(cè)導(dǎo)航tab切換效果,涉及jquery鼠標(biāo)事件及鏈?zhǔn)讲僮鲗?shí)現(xiàn)頁(yè)面元素樣式遍歷切換效果的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08
jquery實(shí)現(xiàn)彈出層效果實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)彈出層效果的方法,實(shí)例分析了jQuery實(shí)現(xiàn)彈出層的技巧,涉及jQuery操作頁(yè)面元素與樣式的技巧,需要的朋友可以參考下2015-05-05

