


JavaScript method to drag and drop elements in web pages_javascript skills
The example in this article describes the method of dragging elements in a web page using JavaScript. Share it with everyone for your reference. The details are as follows:
This code describes the principles and methods of JS drag and drop in detail, which is worth learning and reference.
/** * 跨平台的事件监听函数 * @param {Node} node 需要监听事件的DOM节点 * @param {String} eventType 需要监听的事件类型 * @param {Function} callback 事件监听回调函数 * @type Function 返回值为函数类型 * @return 返回监听回调函数的引用,用于释放监听 */ function bindEvent(node, eventType, callback) { if (node.attachEvent) { if (eventType.indexOf('on')) { eventType = 'on' + eventType;} node.attachEvent(eventType, callback); } else { if (!eventType.indexOf('on')) eventType = eventType.substring(2, eventType.length); node.addEventListener(eventType, callback, false); } return callback; } /** * 跨平台的事件监听卸载函数 * @param {Node} node 需要卸载监听事件的DOM节点 * @param {String} eventType 需要卸载监听的事件类型 * @param {Function} callback 卸载事件监听回调函数 */ function removeEvent(node, eventType, callback) { if (node.detachEvent) { if (eventType.indexOf('on')) { eventType = 'on' + eventType;} node.detachEvent(eventType, callback); } else { if (!eventType.indexOf('on')) eventType = eventType.substring(2, eventType.length); node.removeEventListener(eventType, callback, false); } } /** * 兼容不同定位方式的通用拖动接口 * @param {Node} dragger 需要被拖动的元素 */ //必须告诉系统,哪些元素是可以进行交互,而哪些是不行 function canDrag(dragger) { var drag = bindEvent(dragger,'onmousedown',function(e){ //兼容事件对象 e = e || event; //兼容坐标属性 var pageX = e.clientX || e.pageX; var pageY = e.clientY || e.pageY; //兼容样式对象 var style = dragger.currentStyle || window.getComputedStyle(dragger,null); //当没有设置left和top属性时,IE下默认值为auto var offX = parseInt(style.left) || 0; var offY = parseInt(style.top) || 0; //获取鼠标相对于元素的间距 var offXL = pageX - offX; var offYL = pageY - offY; //为dragger增加onDrag属性,用来存储拖动事件 if (!dragger.onDrag) { //监听拖动事件 dragger.onDrag = bindEvent(document,'onmousemove',function(e){ e = e || event; var x = e.clientX || e.pageX; var y = e.clientY || e.pageY //设置X坐标 dragger.style.left = x - offXL + 'px'; //设置Y坐标 dragger.style.top = y - offYL + 'px'; }); //监听拖动结束事件 dragger.onDragEnd = bindEvent(document,'onmouseup',function(e){ //释放前读取事件对象 var x = e.clientX || e.pageX; var y = e.clientY || e.pageY //释放拖动监听和结束监听 removeEvent(document, 'onmousemove', dragger.onDrag); removeEvent(document, 'onmouseup', dragger.onDragEnd); try { //删除拖动时所用的属性,兼容FF使用 delete dragger.onDrag; delete dragger.onDragEnd; } catch (e) { //删除拖动时所用的属性,兼容IE6使用 dragger.removeAttribute('onDrag'); dragger.removeAttribute('onDragEnd'); } }); } }); return function() { //返回一个可以取消拖动功能的函数引用 //释放拖动监听和结束监听 removeEvent(document, 'onmousemove', dragger.onDrag); removeEvent(document, 'onmouseup', dragger.onDragEnd); try { //删除拖动时所用的属性,兼容FF使用 delete dragger.onDrag; delete dragger.onDragEnd; } catch (e) { //删除拖动时所用的属性,兼容IE6使用 dragger.removeAttribute('onDrag'); dragger.removeAttribute('onDragEnd'); } } }
I hope this article will be helpful to everyone’s JavaScript programming design.

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

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 WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Answer: JavaScript provides a variety of methods for obtaining web page elements, including using ids, tag names, class names, and CSS selectors. Detailed description: getElementById(id): Get elements based on unique id. getElementsByTagName(tag): Gets the element group with the specified tag name. getElementsByClassName(class): Gets the element group with the specified class name. querySelector(selector): Use CSS selector to get the first matching element. querySelectorAll(selector): Get all matches using CSS selector

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
