国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現橫向拖拽導航條功能

2019-11-19 17:31:42
字體:
來源:轉載
供稿:網友

效果如下:

代碼如下:

<!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" >導航一</a></li>  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導航二導航</a></li>  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導航導航導航三</a></li>  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導航導航四</a></li>  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導五</a></li> </ul></div><script type="text/javascript"> $(document).ready(function () {  var range = {x: 0, y: 0};//鼠標元素偏移量  var lastPos = {x: 0, y: 0, x1: 0, y1: 0}; //拖拽對象的四個坐標  var tarPos = {x: 0, y: 0, x1: 0, y1: 0}; //目標元素對象的坐標初始化  var theDiv = null, move = false;  var choose = false; //拖拽對象 拖拽狀態 選中狀態  var theDivId = 0, theDivHeight = 0, theDivHalf = 0;  var tarFirstY = 0; //拖拽對象的索引、高度、的初始化。  var tarDiv = null, tarFirst, tempDiv; //要插入的目標元素的對象, 臨時的虛線對象  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;    //鼠標元素相對偏移量    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'});    // 創建新元素 插入拖拽元素之前的位置(虛線框)    $("<div class='dash'></div>").insertBefore(theDiv);    tempDiv = $(".dash");    $(".dash").css("width" , theDivWidth);    return false   });  });  $(document).mouseup(function (event) {   if (!choose) {    return false;   }   if (!move) {    //恢復對象的初始樣式    theDiv.removeClass("drag-dash");    theDiv.addClass("drag");    tempDiv.remove(); // 刪除新建的虛線div    choose = false;    return false;   }   theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上   //恢復對象的初始樣式   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;   // 拖拽元素隨鼠標移動   theDiv.css({left: lastPos.x + 'px', top: lastPos.y + 'px'});   // 拖拽元素隨鼠標移動 查找插入目標元素   var $main = $('.drag'); // 局部變量:按照重新排列過的順序 再次獲取 各個元素的坐標,   $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; // 第一個元素對象的中心縱坐標    //拖拽對象 移動到第一個位置    if (lastPos.x <= tarFirstX) {     tempDiv.insertBefore(tarFirst);    }    //判斷要插入目標元素的 坐標后, 直接插入    if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1) {     tempDiv.insertAfter(tarDiv);    }   });   return false  }); });</script></body></html>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淳安县| 西乌| 华阴市| 牡丹江市| 荃湾区| 余庆县| 察雅县| 芦溪县| 三都| 红原县| 文成县| 顺平县| 墨玉县| 灵石县| 马公市| 阳高县| 临颍县| 陇南市| 江西省| 吴桥县| 南部县| 太白县| 庄河市| 通城县| 金秀| 崇阳县| 尤溪县| 崇州市| 黎川县| 泸州市| 尖扎县| 靖西县| 和林格尔县| 古田县| 鄂尔多斯市| 天气| 丁青县| 蒙自县| 且末县| 溧水县| 疏勒县|