您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 滨州分类信息网,免费分类信息发布

如何利用js实现横向拖拽导航条功能

2026/1/9 6:46:55发布7次查看
本文主要介绍了js实现横向拖拽导航条功能的方法。具有很好的参考价值,下面一起来看下吧
效果如下:
代码如下:
<!doctype html><html><head> <meta charset="utf-8"> <title>p横向拖拽排序</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <style type="text/css"> body, p { 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>p横向拖拽排序</h1><p 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></p><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 thep = null, move = false; var choose = false; //拖拽对象 拖拽状态 选中状态 var thepid = 0, thepheight = 0, thephalf = 0; var tarfirsty = 0; //拖拽对象的索引、高度、的初始化。 var tarp = null, tarfirst, tempp; //要插入的目标元素的对象, 临时的虚线对象 var initpos = {x: 0, y: 0}; var thepwidth;//拖拽对象的宽度 $(".drag").each(function () { $(this).mousedown(function (event) { choose = true; //拖拽对象 thep = $(this); //记录拖拽元素初始位置 initpos.x = thep.position().left; initpos.y = thep.position().top; //鼠标元素相对偏移量 range.x = event.pagex - thep.position().left; range.y = event.pagey - thep.position().top; thepid = thep.index(); thepwidth = thep.width(); thephalf = thepwidth / 2; thep.removeclass("drag"); thep.addclass("drag-dash"); thep.css({left: initpos.x + 'px', top: initpos.y + 'px'}); // 创建新元素 插入拖拽元素之前的位置(虚线框) $("<p class='dash'></p>").insertbefore(thep); tempp = $(".dash"); $(".dash").css("width" , thepwidth); return false }); }); $(document).mouseup(function (event) { if (!choose) { return false; } if (!move) { //恢复对象的初始样式 thep.removeclass("drag-dash"); thep.addclass("drag"); tempp.remove(); // 删除新建的虚线p choose = false; return false; } thep.insertbefore(tempp); // 拖拽元素插入到 虚线p的位置上 //恢复对象的初始样式 thep.removeclass("drag-dash"); thep.addclass("drag"); tempp.remove(); // 删除新建的虚线p 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 + thepwidth; // 拖拽元素随鼠标移动 thep.css({left: lastpos.x + 'px', top: lastpos.y + 'px'}); // 拖拽元素随鼠标移动 查找插入目标元素 var $main = $('.drag'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标, $main.each(function () { tarp = $(this); tarpos.x = tarp.position().left; tarpos.y = tarp.position().top; tarpos.x1 = tarpos.x + tarp.width() / 2; tarfirst = $main.eq(0); // 获得第一个元素\ tarfirstx = tarfirst.position().left + thephalf; // 第一个元素对象的中心纵坐标 //拖拽对象 移动到第一个位置 if (lastpos.x <= tarfirstx) { tempp.insertbefore(tarfirst); } //判断要插入目标元素的 坐标后, 直接插入 if (lastpos.x >= tarpos.x - thephalf && lastpos.x1 >= tarpos.x1) { tempp.insertafter(tarp); } }); return false }); });</script></body></html>
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
js和css实现鼠标经过弹出一个带缓冲动画渐变效果div框
利用js实现随页面滚动显示/隐藏窗口固定位置元素
以上就是如何利用js实现横向拖拽导航条功能的详细内容。
滨州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product