簡單的jquery拖拽排序效果實(shí)現(xiàn)代碼
更新時(shí)間:2011年09月20日 22:14:39 作者:
寫了簡單的跟隨鼠標(biāo)移動(dòng)效果。這個(gè)拖拽排序的效果的區(qū)別在于: 運(yùn)用了插入insertBefore 和 insertAfter 的方法
步驟:
1.實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)的效果;
2.初始化一個(gè)元素及其坐標(biāo);
3.拖拽對(duì)象的最后坐標(biāo),與元素的坐標(biāo) 進(jìn)行計(jì)算和判斷 來確定 要插入的目標(biāo)元素;
4.用insertBefore 方法 插入到目標(biāo)元素的前面
具體代碼如下:
<!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: 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 };//鼠標(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ì)象
$(".main").each(function(){
$(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","maindash");
// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)
$("<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;
// 拖拽元素隨鼠標(biāo)移動(dòng)
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素隨鼠標(biāo)移動(dòng) 查找插入目標(biāo)元素
var $main = $('.main'); // 局部變量:按照重新排列過的順序 再次獲取 各個(gè)元素的坐標(biāo),
tempDiv = $(".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) {
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
theDiv.attr("class", "main"); //恢復(fù)對(duì)象的初始樣式
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>
1.實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)的效果;
2.初始化一個(gè)元素及其坐標(biāo);
3.拖拽對(duì)象的最后坐標(biāo),與元素的坐標(biāo) 進(jìn)行計(jì)算和判斷 來確定 要插入的目標(biāo)元素;
4.用insertBefore 方法 插入到目標(biāo)元素的前面
具體代碼如下:
復(fù)制代碼 代碼如下:
<!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: 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 };//鼠標(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ì)象
$(".main").each(function(){
$(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","maindash");
// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)
$("<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;
// 拖拽元素隨鼠標(biāo)移動(dòng)
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素隨鼠標(biāo)移動(dòng) 查找插入目標(biāo)元素
var $main = $('.main'); // 局部變量:按照重新排列過的順序 再次獲取 各個(gè)元素的坐標(biāo),
tempDiv = $(".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) {
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
theDiv.attr("class", "main"); //恢復(fù)對(duì)象的初始樣式
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>
您可能感興趣的文章:
- JQuery Dialog(JS 模態(tài)窗口,可拖拽的DIV)
- JQuery UI的拖拽功能實(shí)現(xiàn)方法小結(jié)
- jQuery拖拽div實(shí)現(xiàn)思路
- jQuery使用drag效果實(shí)現(xiàn)自由拖拽div
- 如何使用jQuery Draggable和Droppable實(shí)現(xiàn)拖拽功能
- jQuery插件實(shí)現(xiàn)文件上傳功能(支持拖拽)
- JQuery之拖拽插件實(shí)現(xiàn)代碼
- jquery實(shí)現(xiàn)拖拽調(diào)整Div大小
- jQuery EasyUI API 中文文檔 - Draggable 可拖拽
- jquery實(shí)現(xiàn)拖拽小方塊效果
相關(guān)文章
jquery autocomplete自動(dòng)完成插件的的使用方法
最近剛開始學(xué)jquery,想實(shí)現(xiàn)類似GOOGLE搜索時(shí)自動(dòng)顯示出相關(guān)結(jié)果的效果。于是選擇了使用jquery autocomplete插件。2010-08-08基于Jquery的跨域傳輸數(shù)據(jù)(JSONP)
基于Jquery的跨域傳輸數(shù)據(jù)(JSONP) ,需要的朋友可以參考下。2011-03-03jQuery實(shí)現(xiàn)電梯導(dǎo)航案例詳解(切換?網(wǎng)頁區(qū)域)
日常生活中用手機(jī),電腦瀏覽網(wǎng)頁時(shí),滑到了頁面下端后想返回頂部 或 跳轉(zhuǎn)到頁面別的版塊,用鼠標(biāo)滾動(dòng)很麻煩,網(wǎng)頁電梯導(dǎo)航就可以很方便的精準(zhǔn)到達(dá)目標(biāo)版塊,本文給大家分享jquery電梯導(dǎo)航案例詳解,感興趣的朋友一起看看吧2022-05-05如何基于jQuery實(shí)現(xiàn)五角星評(píng)分
這篇文章主要介紹了如何基于jQuery實(shí)現(xiàn)五角星評(píng)分,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09jquery實(shí)現(xiàn)點(diǎn)擊文字可編輯并修改保存至數(shù)據(jù)庫
網(wǎng)上的方法只有點(diǎn)擊文字編輯并保持,但是沒有完整的代碼寫怎么保存到數(shù)據(jù)庫,本例用一條sql語句保存到數(shù)據(jù)庫2014-04-04基于jQuery實(shí)現(xiàn)Accordion手風(fēng)琴自定義插件
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)Accordion手風(fēng)琴自定義插件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11jquery.uploadify插件在chrome瀏覽器頻繁崩潰解決方法
這篇文章主要介紹了jquery.uploadify插件在chrome瀏覽器頻繁崩潰解決方法,十分的實(shí)用,遇到相同問題,需要解決的朋友可以參考下2015-03-03