JavaScript implements the method of dragging and dropping elements inside a web page

  • 2020-05-30 19:21:22
  • OfStack

This article illustrates how JavaScript can be used to drag and drop elements from a web page. Share with you for your reference. The details are as follows:

This code describes the principle and method of JS drag in detail, which is worth learning and referring to.


/**
*  Cross-platform event listening functions 
* @param {Node} node  You need to listen for events DOM node 
* @param {String} eventType  The type of event you want to listen for 
* @param {Function} callback  The event listens for the callback function 
* @type Function  The return value is of type function 
* @return  Returns a reference to the listener callback function 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 uninstall function 
* @param {Node} node  Need to unmount the listening event DOM node 
* @param {String} eventType  The type of event that needs to be unmounted for listening 
* @param {Function} callback  The unload event listens for the 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);
 }
}
/**
*  Compatible with different positioning methods of the universal drag interface 
* @param {Node} dragger  The element that needs to be dragged 
*/
// You must tell the system which elements are interoperable and which are not 
function canDrag(dragger) {
 var drag = bindEvent(dragger,'onmousedown',function(e){
  // Compatible event object 
  e = e || event;
  // Compatible coordinate attribute 
  var pageX = e.clientX || e.pageX;
  var pageY = e.clientY || e.pageY;
  // Compatible style object 
  var style = dragger.currentStyle || window.getComputedStyle(dragger,null);
  // When not set left and top Properties, IE The default value is auto
  var offX = parseInt(style.left) || 0;
  var offY = parseInt(style.top) || 0;
  // Gets the spacing of the mouse relative to the element 
  var offXL = pageX - offX;
  var offYL = pageY - offY;
  // for dragger increase onDrag Property to store drag events 
  if (!dragger.onDrag) {
   // Listen for drag events 
   dragger.onDrag = bindEvent(document,'onmousemove',function(e){
    e = e || event;
    var x = e.clientX || e.pageX;
    var y = e.clientY || e.pageY
    // Set up the X coordinates 
    dragger.style.left = x - offXL + 'px';
    // Set up the Y coordinates 
    dragger.style.top = y - offYL + 'px';
   });
   // Listen for the drag end event 
   dragger.onDragEnd = bindEvent(document,'onmouseup',function(e){
    // Read the event object before releasing it 
    var x = e.clientX || e.pageX;
    var y = e.clientY || e.pageY
    // Release drag listener and end listener 
    removeEvent(document, 'onmousemove', dragger.onDrag);
    removeEvent(document, 'onmouseup', dragger.onDragEnd);
    try {
     // Delete the properties used when dragging, compatible FF use 
     delete dragger.onDrag;
     delete dragger.onDragEnd;
    } catch (e) {
     // Delete the properties used when dragging, compatible IE6 use 
     dragger.removeAttribute('onDrag');
     dragger.removeAttribute('onDragEnd');
    }
   });
  }
 });
 return function() {
  // return 1 Three function references that can be removed from the drag function 
  // Release drag listener and end listener 
  removeEvent(document, 'onmousemove', dragger.onDrag);
  removeEvent(document, 'onmouseup', dragger.onDragEnd);
  try {
   // Delete the properties used when dragging, compatible FF use 
   delete dragger.onDrag;
   delete dragger.onDragEnd;
  } catch (e) {
   // Delete the properties used when dragging, compatible IE6 use 
   dragger.removeAttribute('onDrag');
   dragger.removeAttribute('onDragEnd');
  }
 }
}

I hope this article is helpful for you to design javascript program.


Related articles: