This article mainly introduces the simple implementation method of jquery drag and drop sorting, involving related skills of jQuery responding to mouse events to dynamically operate page elements. Friends in need can refer to the following
The example of this article describes jquery drag and drop sorting Simple implementation method. Share it with everyone for your reference, the details are as follows:
The screenshot of the running effect is as follows:
Originally there was no new action, but after analyzing the code, I found that it is easy to enhance~~
The code is as follows:
nbsp;HTML> <meta> <title>测试的拖拽功能</title> <style> body, p { margin: 0; paading: 0; font-size: 12px; } body { width:100%; margin: 0 auto; } ul, li { margin: 0; padding: 0; list-style: none; } .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; } .drag_module_box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } .drag_module_box1 { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } .drag_module_main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } .drag_module_maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } .drag_module_hide { width: 600px; height: 80px; margin-bottom: 5px; } .drag_module_dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; }; </style> <script></script> <script> $(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 thepId =0, thepHeight = 0, thepHalf = 0; tarFirstY = 0; //拖拽对象的索引、高度、的初始化。 var tarp = null, tarFirst, tempp; //要插入的目标元素的对象, 临时的虚线对象 function loopbox(){ //循环初始化 $(".drag_module_box").find(".drag_module_main").each(function(){ console.log( 'find' ); $(this).mousedown(function (event){ //拖拽对象 thep = $(this); //鼠标元素相对偏移量 range.x = event.pageX - thep.offset().left; range.y = event.pageY - thep.offset().top; thepId = thep.index(); thepHeight = thep.height(); thepHalf = thepHeight/2; move = true; thep.attr("class","drag_module_maindash"); // 创建新元素 插入拖拽元素之前的位置(虚线框) $("<p class='drag_module_dash'>").insertBefore(thep); }); }); } loopbox(); $(".drag_module_box").mousemove(function(event) { console.log( 'mousemove' ); if (!move) return false; lastPos.x = event.pageX - range.x; lastPos.y = event.pageY - range.y; lastPos.y1 = lastPos.y + thepHeight; // 拖拽元素随鼠标移动 thep.css({left: lastPos.x + 'px',top: lastPos.y + 'px'}); // 拖拽元素随鼠标移动 查找插入目标元素 var $main = $('.drag_module_main'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标, tempp = $(".drag_module_dash"); //获得临时 虚线框的对象 $main.each(function () { tarp = $(this); tarPos.x = tarp.offset().left; tarPos.y = tarp.offset().top; tarPos.y1 = tarPos.y + tarp.height()/2; tarFirst = $main.eq(0); // 获得第一个元素 tarFirstY = tarFirst.offset().top + thepHalf ; // 第一个元素对象的中心纵坐标 //拖拽对象 移动到第一个位置 if (lastPos.y <= tarFirstY) { tempp.insertBefore(tarFirst); } //判断要插入目标元素的 坐标后, 直接插入 if (lastPos.y >= tarPos.y - thepHalf && lastPos.y1 >= tarPos.y1 ) { tempp.insertAfter(tarp); } }); }).mouseup(function(event) { console.log( 'mouseup' ); if(thep==null) return false; thep.insertBefore(tempp); // 拖拽元素插入到 虚线p的位置上 thep.attr("class", "drag_module_main"); //恢复对象的初始样式 $('.drag_module_dash').remove(); // 删除新建的虚线p move=false; }); $("#drag_module_insert").click(function(){ $("#drag_module_box1").html($("#drag_module_box1").html()+$("#drag_module_box2").html()); loopbox(); }); $("#drag_module_seque").click(function(){ $(".drag_module_box").find(".drag_module_main").each(function(){ console.log($(this).attr('id')); }); }); }); </script> <p> </p><p>p1</p> <p>p2</p> <p>p3</p> <p>p4</p> <p>p5</p> <p>p6</p> <p> </p><p>p7</p> <input> <input>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the case of JS getting the iframe of the parent window and the child window
jQuery getting the iframe of the parent window and the child window Detailed case explanation
#How jQuery calculates the width and height of the iframe window
The above is the detailed content of jquery operation multi-function form. For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
