js實現(xiàn)橫向拖拽導(dǎo)航條功能
更新時間:2017年02月17日 15:02:09 作者:還好
本文主要介紹了js實現(xiàn)橫向拖拽導(dǎo)航條功能的方法。具有很好的參考價值,下面跟著小編一起來看下吧
效果如下:

代碼如下:
<!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 {
position: relative;
margin-left: 15px;
padding: 10px;
padding-right: 0px;
width: 810px;
border: blue solid 1px;
}
.box ul{
list-style: none;
overflow: hidden;
padding: 0;
margin:0;
}
.drag {
float: left;
border: #000 solid 1px;
text-align: center;
}
.box ul li a{
display: block;
padding: 10px 25px;
}
.drag-dash {
position: absolute;
border: #000 solid 1px;
background: #ececec;
}
.dash {
float: left;
border: 1px solid transparent;
}
</style>
</head>
<body>
<h1>div橫向拖拽排序</h1>
<div class="box">
<ul>
<li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航一</a></li>
<li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航二導(dǎo)航</a></li>
<li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航導(dǎo)航導(dǎo)航三</a></li>
<li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航導(dǎo)航四</a></li>
<li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)五</a></li>
</ul>
</div>
<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}; //拖拽對象的四個坐標(biāo)
var tarPos = {x: 0, y: 0, x1: 0, y1: 0}; //目標(biāo)元素對象的坐標(biāo)初始化
var theDiv = null, move = false;
var choose = false; //拖拽對象 拖拽狀態(tài) 選中狀態(tài)
var theDivId = 0, theDivHeight = 0, theDivHalf = 0;
var tarFirstY = 0; //拖拽對象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對象, 臨時的虛線對象
var initPos = {x: 0, y: 0};
var theDivWidth;//拖拽對象的寬度
$(".drag").each(function () {
$(this).mousedown(function (event) {
choose = true;
//拖拽對象
theDiv = $(this);
//記錄拖拽元素初始位置
initPos.x = theDiv.position().left;
initPos.y = theDiv.position().top;
//鼠標(biāo)元素相對偏移量
range.x = event.pageX - theDiv.position().left;
range.y = event.pageY - theDiv.position().top;
theDivId = theDiv.index();
theDivWidth = theDiv.width();
theDivHalf = theDivWidth / 2;
theDiv.removeClass("drag");
theDiv.addClass("drag-dash");
theDiv.css({left: initPos.x + 'px', top: initPos.y + 'px'});
// 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框)
$("<div class='dash'></div>").insertBefore(theDiv);
tempDiv = $(".dash");
$(".dash").css("width" , theDivWidth);
return false
});
});
$(document).mouseup(function (event) {
if (!choose) {
return false;
}
if (!move) {
//恢復(fù)對象的初始樣式
theDiv.removeClass("drag-dash");
theDiv.addClass("drag");
tempDiv.remove(); // 刪除新建的虛線div
choose = false;
return false;
}
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
//恢復(fù)對象的初始樣式
theDiv.removeClass("drag-dash");
theDiv.addClass("drag");
tempDiv.remove(); // 刪除新建的虛線div
move = false;
choose = false;
return 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)移動
theDiv.css({left: lastPos.x + 'px', top: lastPos.y + 'px'});
// 拖拽元素隨鼠標(biāo)移動 查找插入目標(biāo)元素
var $main = $('.drag'); // 局部變量:按照重新排列過的順序 再次獲取 各個元素的坐標(biāo),
$main.each(function () {
tarDiv = $(this);
tarPos.x = tarDiv.position().left;
tarPos.y = tarDiv.position().top;
tarPos.x1 = tarPos.x + tarDiv.width() / 2;
tarFirst = $main.eq(0); // 獲得第一個元素\
tarFirstX = tarFirst.position().left + theDivHalf; // 第一個元素對象的中心縱坐標(biāo)
//拖拽對象 移動到第一個位置
if (lastPos.x <= tarFirstX) {
tempDiv.insertBefore(tarFirst);
}
//判斷要插入目標(biāo)元素的 坐標(biāo)后, 直接插入
if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1) {
tempDiv.insertAfter(tarDiv);
}
});
return false
});
});
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
您可能感興趣的文章:
- JS實現(xiàn)選中當(dāng)前菜單后高亮顯示的導(dǎo)航條效果
- CSS3+Js實現(xiàn)響應(yīng)式導(dǎo)航條
- 仿客齊集首頁導(dǎo)航條DIV+CSS+JS [代碼實例]
- 基于JS代碼實現(xiàn)導(dǎo)航條彈出式懸浮菜單
- vuejs 切換導(dǎo)航條高亮(路由菜單高亮)的方法示例
- JS+CSS實現(xiàn)鼠標(biāo)滑過時動態(tài)翻滾的導(dǎo)航條效果
- JS組件Bootstrap導(dǎo)航條使用方法詳解
- javascript實現(xiàn)的鼠標(biāo)懸停時動態(tài)翻滾的導(dǎo)航條
- 純JS實現(xiàn)彈性導(dǎo)航條效果
- JS實現(xiàn)網(wǎng)頁導(dǎo)航條特效
相關(guān)文章
JAVASCRIPT style 中visibility和display之間的區(qū)別
visibility屬性用來確定元素是顯示還是隱藏的,這用visibility="visible|hidden"來表示(visible表示顯示,hidden表示隱藏)。2010-01-01
JavaScript創(chuàng)建防篡改對象的方法分析
這篇文章主要介紹了JavaScript創(chuàng)建防篡改對象的方法,結(jié)合具體實例形式分析了javascript基于不可擴(kuò)展對象、密封的對象和凍結(jié)的對象實現(xiàn)防篡改對象的相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
微信小程序開發(fā)中var that =this的用法詳解
這篇文章主要介紹了微信小程序開發(fā)中var that =this的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
JS實現(xiàn)經(jīng)典的中國地區(qū)三級聯(lián)動下拉菜單功能實例【測試可用】
這篇文章主要介紹了JS實現(xiàn)經(jīng)典的中國地區(qū)三級聯(lián)動下拉菜單功能,結(jié)合完整實例形式分析了javascript基于事件響應(yīng)實現(xiàn)頁面元素動態(tài)變換的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
深入理解JavaScript系列(14) 作用域鏈介紹(Scope Chain)
在第12章關(guān)于變量對象的描述中,我們已經(jīng)知道一個執(zhí)行上下文 的數(shù)據(jù)(變量、函數(shù)聲明和函數(shù)的形參)作為屬性存儲在變量對象中2012-04-04
JavaScript之創(chuàng)意時鐘項目(實例講解)
下面小編就為大家?guī)硪黄狫avaScript之創(chuàng)意時鐘項目。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
javascript中加var和不加var的區(qū)別 你真的懂嗎
var 語句用于聲明變量,本文給大家介紹javascript 中加’var‘和不加'var'的區(qū)別,涉及到j(luò)avascript var相關(guān)知識,對javascript var相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01

