Use JavaScript to drag elements in a webpage.

Source: Internet
Author: User

Use JavaScript to drag elements in a webpage.

This example describes how to drag elements from a webpage using JavaScript. Share it with you for your reference. The details are as follows:

This code details the principles and methods of JS drag and drop, which is worth learning and learning.

/*** Cross-platform event listening Function * @ param {Node} node the DOM Node that needs to listen to the event * @ param {String} eventType the event type to listen to * @ param {Function} callback event listening callback Function * @ type Function returns the Function type * @ return returns the reference of the listener callback Function, used to release the listener */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 ;} /*** cross-platform event listening unload function * @ param {Node} node the DOM Node that needs to uninstall the listening event * @ param {String} eventType the event type to be uninstalled * @ param {Function} callback uninstall event listening callback function */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 );}} /*** universal drag interface compatible with different positioning Methods * @ param {Node} the elements to be dragged by dragger * // The system must be notified of which elements can be interacted, which of the following functions does not work? function canDrag (dragger) {var drag = bindEvent (dragger, 'onmousedown', function (e) {// compatible with event object e = e | event; // compatible with the coordinate property var pageX = e. clientX | e. pageX; var pageY = e. clientY | e. pageY; // Compatible with the style object var style = dragger. currentStyle | window. getComputedStyle (dragger, null); // if left and top attributes are not set, the default value of IE is auto var offX = parseInt (style. left) | 0; var offY = parseInt (style. top) | 0; // obtain the spacing between the mouse and the element var offXL = pageX-offX; var offYL = pageY-offY; // Add the onDrag attribute to the dragger, used to store the drag event if (! Dragger. onDrag) {// listen to the dragger drag event. onDrag = bindEvent (document, 'onmousemove ', function (e) {e = e | event; var x = e. clientX | e. pageX; var y = e. clientY | e. pageY // set X coordinate dragger. style. left = x-offXL + 'px '; // you can specify the Y coordinate to dragger. style. top = y-offYL + 'px ';}); // listen to the dragger Event After dragging. onDragEnd = bindEvent (document, 'onmouseup', function (e) {// read event object var x = e before release. clientX | e. pageX; var y = e. clientY | e. pageY // release the removeEvent (document, 'onmousemove ', dragger. onDrag); removeEvent (document, 'onmouseup', dragger. onDragEnd); try {// delete the attribute used when dragging, compatible with FF using delete dragger. onDrag; delete dragger. onDragEnd;} catch (e) {// Delete the attributes used for dragging. compatible with IE6's use of dragger. removeAttribute ('ondrag'); dragger. removeAttribute ('ondragend') ;}}}}); return function () {// return a function reference that can cancel the drag function // release the drag listener and terminate the listener removeEvent (document, 'onmousemove ', dragger. onDrag); removeEvent (document, 'onmouseup', dragger. onDragEnd); try {// delete the attribute used when dragging, compatible with FF using delete dragger. onDrag; delete dragger. onDragEnd;} catch (e) {// Delete the attributes used for dragging. compatible with IE6's use of dragger. removeAttribute ('ondrag'); dragger. removeAttribute ('ondragend ');}}}

I hope this article will help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.