jQuery實(shí)現(xiàn)div橫向拖拽排序的簡(jiǎn)單實(shí)例
實(shí)例如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>div橫向拖拽排序</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<style type="text/css">
body,div{
padding: 0px;
margin: 0px;
}
.box {
margin-left: 15px;
padding : 10px;
padding-right: 0px;
width : 810px;
height : 150px;
border : blue solid 1px;
}
.horizontal-div{
float : left;
margin-right: 10px;
border: #000 solid 1px;
text-align: center;
width : 150px;
height : 150px;
}
.horizontal-div-dash {
position: absolute;
width: 150px;
height: 150px;
margin-right: 10px;
border: 1px dashed blue;
background: #ececec;
opacity: 0.7;
}
.dash {
float : left;
width: 150px;
height: 150px;
margin-right: 10px;
border: 1px dashed #f00;
};
</style>
<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; choose = false; //拖拽對(duì)象 拖拽狀態(tài) 選中狀態(tài)
var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽對(duì)象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對(duì)象, 臨時(shí)的虛線對(duì)象
var initPos = {x: 0, y: 0};
$(".horizontal-div").each(function(){
$(this).mousedown(function (event){
choose = true;
//拖拽對(duì)象
theDiv = $(this);
//記錄拖拽元素初始位置
initPos.x = theDiv.offset().left;
initPos.y = theDiv.offset().top;
//鼠標(biāo)元素相對(duì)偏移量
range.x = event.pageX - theDiv.offset().left;
range.y = event.pageY - theDiv.offset().top;
theDivId = theDiv.index();
theDivWidth = theDiv.width();
theDivHalf = theDivWidth/2;
theDiv.attr("class","horizontal-div-dash");
theDiv.css({left: initPos.x + 'px',top: initPos.y + 'px'});
// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)
$("<div class='dash'></div>").insertBefore(theDiv);
tempDiv = $(".dash");
});
});
$(document).mouseup(function(event) {
if(!choose){return false;}
if(!move){
theDiv.attr("class", "horizontal-div");
tempDiv.remove(); // 刪除新建的虛線div
choose = false;
return false;
}
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
theDiv.attr("class", "horizontal-div"); //恢復(fù)對(duì)象的初始樣式
tempDiv.remove(); // 刪除新建的虛線div
move = false;
choose = false;
}).mousemove(function(event) {
if(!choose) return false;
move = true;
lastPos.x = event.pageX - range.x;
lastPos.y = event.pageY - range.y;
lastPos.x1 = lastPos.x + theDivWidth;
// 拖拽元素隨鼠標(biāo)移動(dòng)
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素隨鼠標(biāo)移動(dòng) 查找插入目標(biāo)元素
var $main = $('.horizontal-div'); // 局部變量:按照重新排列過(guò)的順序 再次獲取 各個(gè)元素的坐標(biāo),
$main.each(function () {
tarDiv = $(this);
tarPos.x = tarDiv.offset().left;
tarPos.y = tarDiv.offset().top;
tarPos.x1 = tarPos.x + tarDiv.width()/2;
tarFirst = $main.eq(0); // 獲得第一個(gè)元素
tarFirstX = tarFirst.offset().left + theDivHalf ; // 第一個(gè)元素對(duì)象的中心縱坐標(biāo)
//拖拽對(duì)象 移動(dòng)到第一個(gè)位置
if (lastPos.x <= tarFirstX) {
tempDiv.insertBefore(tarFirst);
}
//判斷要插入目標(biāo)元素的 坐標(biāo)后, 直接插入
if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1 ) {
tempDiv.insertAfter(tarDiv);
}
});
});
});
</script>
</head>
<body>
<h1>div橫向拖拽排序</h1>
<div class="box">
<div class="horizontal-div" id="div1">div1</div>
<div class="horizontal-div" id="div2">div2</div>
<div class="horizontal-div" id="div3">div3</div>
<div class="horizontal-div" id="div4">div4</div>
<div class="horizontal-div" id="div5">div5</div>
</div>
</body>
</html>
以上這篇jQuery實(shí)現(xiàn)div橫向拖拽排序的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 簡(jiǎn)單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)動(dòng)態(tài))
- 針對(duì)后臺(tái)列表table拖拽比較實(shí)用的jquery拖動(dòng)排序
- 通過(guò)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)文章
ASP.NET中基于JQUERY的高性能的TreeView補(bǔ)充
根據(jù) 假正經(jīng)哥哥 的博客整理,我第一次使用的另外的TREEVIEW,可是 做出來(lái)的效果 太卡,不知道什么原因。這次用的假正經(jīng)哥哥的這篇文章。2011-02-02
基于jQuery實(shí)現(xiàn)的查看全文功能【實(shí)用】
本文分享了利用jQuery實(shí)現(xiàn)的查看全文功能:文本內(nèi)容少于四行,不顯示查看全文;超過(guò)五行時(shí)才顯示出來(lái)并有此功能;很實(shí)用,下面就跟小編一起來(lái)看看吧2016-12-12
jquery自定義插件——window的實(shí)現(xiàn)【示例代碼】
下面小編就為大家?guī)?lái)一篇jquery自定義插件——window的實(shí)現(xiàn)【示例代碼】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05
jQuery實(shí)現(xiàn)購(gòu)物車(chē)全功能
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)購(gòu)物車(chē)全功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
html5以及jQuery實(shí)現(xiàn)本地圖片上傳前的預(yù)覽代碼實(shí)例講解
這篇文章主要介紹了html5以及jQuery實(shí)現(xiàn)本地圖片上傳前的預(yù)覽代碼實(shí)例講解,圖文代碼實(shí)例講解的很清晰,有感興趣的同學(xué)可以研究下2021-03-03
使用jquery+CSS3實(shí)現(xiàn)仿windows10開(kāi)始菜單的下拉導(dǎo)航菜單特效
本文是基于jquery和css3實(shí)現(xiàn)的仿windows10開(kāi)始菜單的下拉導(dǎo)航菜單特效,代碼超簡(jiǎn)單,感興趣的朋友一起看看吧2015-09-09
jquery三大不同版本之間區(qū)別專(zhuān)家講解
jQuery是一個(gè)快速、簡(jiǎn)潔的JavaScript框架,目前jQuery有1.x、2.x、3.x三個(gè)版本,在jQuery官網(wǎng)(http://jquery.com/)可以查看,2.x、3.x不是?1.x?的后續(xù)或者升級(jí)版本,他們是同時(shí)更新迭代的,現(xiàn)在使用最廣的版本都是1.x的2023-09-09

