js如何實(shí)現(xiàn)橫向拖拽導(dǎo)航條功能

這篇文章給大家分享的是有關(guān)js如何實(shí)現(xiàn)橫向拖拽導(dǎo)航條功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

在泰安等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),外貿(mào)營銷網(wǎng)站建設(shè),泰安網(wǎng)站建設(shè)費(fèi)用合理。

效果如下:

js如何實(shí)現(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>
<h2>div橫向拖拽排序</h2>
<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}; //拖拽對(duì)象的四個(gè)坐標(biāo)
  var tarPos = {x: 0, y: 0, x1: 0, y1: 0}; //目標(biāo)元素對(duì)象的坐標(biāo)初始化
  var theDiv = null, move = false;
  var choose = false; //拖拽對(duì)象 拖拽狀態(tài) 選中狀態(tài)
  var theDivId = 0, theDivHeight = 0, theDivHalf = 0;
  var tarFirstY = 0; //拖拽對(duì)象的索引、高度、的初始化。
  var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對(duì)象, 臨時(shí)的虛線對(duì)象
  var initPos = {x: 0, y: 0};
  var theDivWidth;//拖拽對(duì)象的寬度
  $(".drag").each(function () {
   $(this).mousedown(function (event) {
    choose = true;
    //拖拽對(duì)象
    theDiv = $(this);
    //記錄拖拽元素初始位置
    initPos.x = theDiv.position().left;
    initPos.y = theDiv.position().top;
    //鼠標(biāo)元素相對(duì)偏移量
    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ù)對(duì)象的初始樣式
    theDiv.removeClass("drag-dash");
    theDiv.addClass("drag");
    tempDiv.remove(); // 刪除新建的虛線div
    choose = false;
    return false;
   }
   theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上
   //恢復(fù)對(duì)象的初始樣式
   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)移動(dòng)
   theDiv.css({left: lastPos.x + 'px', top: lastPos.y + 'px'});
   // 拖拽元素隨鼠標(biāo)移動(dòng) 查找插入目標(biāo)元素
   var $main = $('.drag'); // 局部變量:按照重新排列過的順序 再次獲取 各個(gè)元素的坐標(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); // 獲得第一個(gè)元素\
    tarFirstX = tarFirst.position().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);
    }
   });
   return false
  });
 });
</script>
</body>
</html>

感謝各位的閱讀!關(guān)于“js如何實(shí)現(xiàn)橫向拖拽導(dǎo)航條功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享標(biāo)題:js如何實(shí)現(xiàn)橫向拖拽導(dǎo)航條功能
網(wǎng)頁URL:http://muchs.cn/article26/isjcjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、營銷型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化、域名注冊(cè)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)