js实现横向拖拽导航条功能


Posted in Javascript onFebruary 17, 2017

效果如下:

js实现横向拖拽导航条功能

代码如下:

<!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>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
Firebug 字幕文件JSON地址获取代码
Oct 28 Javascript
js 使FORM表单的所有元素不可编辑的示例代码
Oct 17 Javascript
JavaScript link方法入门实例(给字符串加上超链接)
Oct 17 Javascript
JavaScript实现为指定对象添加多个事件处理程序的方法
Apr 17 Javascript
javascript实现移动端上的触屏拖拽功能
Mar 04 Javascript
JS代码防止SQL注入的方法(超简单)
Apr 12 Javascript
正则验证小数点后面只能有两位数的方法
Feb 28 Javascript
vue货币过滤器的实现方法
Apr 01 Javascript
vue2.X组件学习心得(新手必看篇)
Jul 05 Javascript
jquery.validate.js 多个相同name的处理方式
Jul 10 jQuery
js实现点击展开隐藏效果(实例代码)
Sep 28 Javascript
JavaScript常用事件介绍
Jan 21 Javascript
js转换对象为xml
Feb 17 #Javascript
EsLint入门学习教程
Feb 17 #Javascript
使用bat打开多个cmd窗口执行gulp、node
Feb 17 #Javascript
AngularJS执行流程详解
Feb 17 #Javascript
详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例
Feb 17 #Javascript
bootstrap Validator 模态框、jsp、表单验证 Ajax提交功能
Feb 17 #Javascript
走进AngularJs之过滤器(filter)详解
Feb 17 #Javascript
You might like
php过滤html中的其他网站链接的方法(域名白名单功能)
2014/04/24 PHP
模板引擎smarty工作原理以及使用示例
2014/05/25 PHP
PHP常用算法和数据结构示例(必看篇)
2017/03/15 PHP
Laravel框架控制器的middleware中间件用法分析
2019/09/30 PHP
浅谈laravel5.5 belongsToMany自身的正确用法
2019/10/17 PHP
jQuery源码分析之Event事件分析
2010/06/07 Javascript
JS案例分享之金额小写转大写
2014/05/15 Javascript
javascript动态修改Li节点值的方法
2015/01/20 Javascript
javascript用函数实现对象的方法
2015/05/14 Javascript
js限制文本框只能输入中文的方法
2015/08/11 Javascript
javascript性能优化之事件委托实例详解
2015/12/12 Javascript
使用jQuery处理AJAX请求的基础学习教程
2016/05/10 Javascript
移动端滑动插件Swipe教程
2016/10/16 Javascript
vue底部加载更多的实例代码
2018/06/29 Javascript
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2014/08/22 Python
利用Python进行异常值分析实例代码
2017/12/07 Python
python数字图像处理实现直方图与均衡化
2018/05/04 Python
tensorflow 获取变量&amp;打印权值的实例讲解
2018/06/14 Python
基于多进程中APScheduler重复运行的解决方法
2019/07/22 Python
Python笔记之观察者模式
2019/11/20 Python
django模型动态修改参数,增加 filter 字段的方式
2020/03/16 Python
Python环境下安装PyGame和PyOpenGL的方法
2020/03/25 Python
基于Python的自媒体小助手---登录页面的实现代码
2020/06/29 Python
澳大利亚墨水站Ink Station:墨水和碳粉打印机墨盒
2019/03/24 全球购物
澳大利亚窗帘商店:Curtain Wonderland
2019/12/01 全球购物
DOUGLAS荷兰:购买香水和化妆品
2020/10/24 全球购物
blueseventy官网:铁人三项和比赛泳衣
2021/02/06 全球购物
下面代码从性能上考虑,有什么问题
2015/04/03 面试题
大学生毕业求职的自我评价
2013/09/29 职场文书
教师辞职报告范文
2014/01/20 职场文书
自我鉴定书
2014/03/24 职场文书
英语课前三分钟演讲稿(6篇)
2014/09/13 职场文书
2014年人事工作总结范文
2014/11/19 职场文书
班主任经验交流材料
2014/12/16 职场文书
2015安全保卫工作总结
2015/04/25 职场文书
2016年小学六一儿童节活动总结
2016/04/06 职场文书