亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

效果如下:

代碼如下:

<!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}; //拖拽對(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'); // 局部變量:按照重新排列過(guò)的順序 再次獲取 各個(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>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • 在JavaScript中模擬類(class)及類的繼承關(guān)系

    在JavaScript中模擬類(class)及類的繼承關(guān)系

    眾所周知,JavaScript中沒有類,然而我們卻可以動(dòng)手實(shí)現(xiàn)一個(gè)擁有繼承特性的類,所以接下來(lái)我們要討論的便是在JavaScript中模擬類(class)及類的繼承關(guān)系:
    2016-05-05
  • javascript?中動(dòng)畫制作方法?animate()屬性

    javascript?中動(dòng)畫制作方法?animate()屬性

    這篇文章主要介紹了javascript?中動(dòng)畫制作方法?animate()屬性,animate是所有dom元素都有的方法,可以用來(lái)最做過(guò)度動(dòng)畫,關(guān)鍵幀動(dòng)畫,下面文章的相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • 微信小程序?qū)崿F(xiàn)的繪制table表格功能示例

    微信小程序?qū)崿F(xiàn)的繪制table表格功能示例

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)的繪制table表格功能,涉及微信小程序數(shù)據(jù)讀取及界面布局相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • JavaScript實(shí)現(xiàn)刷新不重記的倒計(jì)時(shí)

    JavaScript實(shí)現(xiàn)刷新不重記的倒計(jì)時(shí)

    網(wǎng)上關(guān)于JavaScript實(shí)現(xiàn)倒計(jì)時(shí)的文章有很多,但是一般都是刷新后會(huì)重新開始計(jì)時(shí),可是我們有的時(shí)候會(huì)需要刷新卻不重新計(jì)算的倒計(jì)時(shí),這該怎么做呢?下面我們一起來(lái)看看這種倒計(jì)時(shí)怎么實(shí)現(xiàn)吧。
    2016-08-08
  • javascript單頁(yè)面手勢(shì)滑屏切換原理詳解

    javascript單頁(yè)面手勢(shì)滑屏切換原理詳解

    這篇文章主要為大家詳細(xì)介紹了javascript單頁(yè)面手勢(shì)滑屏切換原理,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 全系IE支持Bootstrap的解決方法

    全系IE支持Bootstrap的解決方法

    用了bootstrap模版搭建的網(wǎng)站,在IE7中打不開,在IE8中背景圖片都不顯示,內(nèi)容排列也出現(xiàn)問(wèn)題,在IE9中表現(xiàn)的最好,在IE11中出現(xiàn)彈出層中的圖片無(wú)法顯示,那么這些兼容性怎么去解決
    2015-10-10
  • JavaScript簡(jiǎn)單實(shí)現(xiàn)網(wǎng)頁(yè)回到頂部功能

    JavaScript簡(jiǎn)單實(shí)現(xiàn)網(wǎng)頁(yè)回到頂部功能

    JavaScript簡(jiǎn)單實(shí)現(xiàn)網(wǎng)頁(yè)回到頂部功能,大家可以參考一下
    2013-11-11
  • ajax java 實(shí)現(xiàn)自動(dòng)完成功能

    ajax java 實(shí)現(xiàn)自動(dòng)完成功能

    都知道百度建議是用ajax做的,想要做的快速穩(wěn)定,可復(fù)制可移植就不容易了,花時(shí)間研究還不如自己來(lái)寫。根據(jù)一個(gè)pdf文檔提供的資料,用了小半天時(shí)間,終于實(shí)現(xiàn)了。在此與大家分享
    2012-12-12
  • javascript垃圾收集機(jī)制的原理分析

    javascript垃圾收集機(jī)制的原理分析

    javascript具有自動(dòng)垃圾收集機(jī)制,執(zhí)行環(huán)境會(huì)負(fù)責(zé)管理代碼執(zhí)行過(guò)程中使用的內(nèi)存。在編寫javascript程序時(shí),開發(fā)人員不用再關(guān)心內(nèi)存使用問(wèn)題,所需內(nèi)存的分配以及無(wú)用內(nèi)存的回收完全實(shí)現(xiàn)了自動(dòng)管理。本文將詳細(xì)介紹javascript的垃圾收集機(jī)制
    2016-12-12
  • 取得窗口大小 兼容所有瀏覽器的js代碼

    取得窗口大小 兼容所有瀏覽器的js代碼

    我們首先把window.innerWidth和window.innerHeight的值分別付給了pageWidth和pageHeight。
    2011-08-08

最新評(píng)論