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 相关文章推荐
用javascript实现兼容IE7的类库 IE7_0_9.zip提供下载
Aug 08 Javascript
得到form下的所有的input的js代码
Nov 07 Javascript
jQuery+css实现的tab切换标签(兼容各浏览器)
Jan 28 Javascript
jQuery实现手机自定义弹出输入框
Jun 13 Javascript
js HTML5手机刮刮乐代码
Sep 29 Javascript
详解使用vue-cli脚手架初始化Vue项目下的项目结构
Mar 08 Javascript
react-native封装插件swiper的使用方法
Mar 20 Javascript
详解如何使用webpack打包JS
Jun 21 Javascript
PHP实现基于Redis的MessageQueue队列封装操作示例
Feb 02 Javascript
node.js使用net模块创建服务器和客户端示例【基于TCP协议】
Feb 14 Javascript
不刷新网页就能链接新的js文件方法总结
Mar 01 Javascript
JavaScript 监听组合按键思路及代码实现
Jul 28 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获取当前网址url并替换参数或网址的方法
2010/06/06 PHP
浅谈PHP中单引号和双引号到底有啥区别呢?
2015/03/04 PHP
php构造函数与析构函数
2016/04/23 PHP
解析PHP之提取多维数组指定列的方法
2017/01/03 PHP
Yii2第三方类库插件Imagine的安装和使用
2017/07/06 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
JavaScript 字符串处理函数使用小结
2010/12/02 Javascript
获取鼠标在div中的相对位置的实现代码
2013/12/30 Javascript
jquery的attr方法禁用表单元素禁用输入内容
2014/06/23 Javascript
再谈javascript注入 黑客必备!
2016/09/14 Javascript
jQuery轻松实现无缝轮播效果
2017/03/22 jQuery
微信小程序request出现400的问题解决办法
2017/05/23 Javascript
JavaScript对象_动力节点Java学院整理
2017/06/23 Javascript
详解用webpack把我们的业务模块分开打包的方法
2017/07/20 Javascript
laravel5.3 vue 实现收藏夹功能实例详解
2018/01/21 Javascript
Vue2 模板template的四种写法总结
2018/02/23 Javascript
解决vue-cli + webpack 新建项目出错的问题
2018/03/20 Javascript
简单的三步vuex入门
2018/05/20 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
vue项目中,main.js,App.vue,index.html的调用方法
2018/09/20 Javascript
vue基于better-scroll实现左右联动滑动页面
2020/06/30 Javascript
解决vue项目中遇到 Cannot find module ‘chalk‘ 报错的问题
2020/11/05 Javascript
[01:05:40]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS DT第三场
2014/05/24 DOTA
从零学Python之入门(四)运算
2014/05/27 Python
Python切片用法实例教程
2014/09/08 Python
Python实现简单过滤文本段的方法
2017/05/24 Python
Python subprocess模块详细解读
2018/01/29 Python
Python 实现OpenCV格式和PIL.Image格式互转
2020/01/09 Python
目标管理责任书
2014/04/15 职场文书
2014广电局实施党的群众路线教育实践活动方案思想汇报
2014/09/22 职场文书
经费申请报告范文
2015/05/18 职场文书
初一军训感言
2015/08/01 职场文书
2016年机关单位节能宣传周活动总结
2016/04/05 职场文书
详解python字符串驻留技术
2021/05/21 Python
解决Swagger2返回map复杂结构不能解析的问题
2021/07/02 Java/Android
html原生table实现合并单元格以及合并表头的示例代码
2023/05/07 HTML / CSS