


How to use js to implement horizontal drag and drop navigation bar function
This article mainly introduces the js method to implement the horizontal drag and drop navigation bar function. It has a very good reference value, let’s take a look at it
The effect is as follows:
The code is as follows:
<!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>
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to PHP Chinese website!
Related recommendations:
JS and CSS to pop up a DIV box with a buffered animation gradient effect after the mouse passes
##Use JS Achieve displaying/hiding window fixed position elements as the page scrolls
The above is the detailed content of How to use js to implement horizontal drag and drop navigation bar function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JavaScript to implement the drag and zoom function of images? In modern web development, dragging and zooming images is a common requirement. By using JavaScript, we can easily add dragging and zooming functions to images to provide a better user experience. In this article, we will introduce how to use JavaScript to implement this function, with specific code examples. HTML structure First, we need a basic HTML structure to display pictures and add

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:
