Home > Web Front-end > JS Tutorial > body text

JavaScript method to drag and drop elements in web pages_javascript skills

WBOY
Release: 2016-05-16 16:03:54
Original
1218 people have browsed it

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');
  }
 }
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!