Home  >  Article  >  Web Front-end  >  A brief analysis of a js drag effect class and dom-drag.js_javascript skills

A brief analysis of a js drag effect class and dom-drag.js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:22:491021browse

This is a very simple class that only implements the drag and drop function. Of course, the code remains original and concise. The following is the code of this class library:
Code

Copy code The code is as follows:

/************************************************
* Drag.js
* Author: Oak Lodge 07.17.2010
* http://www.cnblogs.com/babyzone2004/
* Usage: Drag.initDrag(id); id is the id of the tag Name
************************************************ ****/
var Drag={
dragObject:null,
mouseOffset:null,
initDrag:function(item){
if(item){
this.item=document.getElementById(item);
this.item.onmousedown=function(evnt ){
document.onmousemove=Drag.mouseMove;
document.onmouseup=Drag.mouseUp;
Drag.dragObject=this;
Drag.mouseOffset=Drag.getMouseOffset(this,evnt);
return false;
}
}
},
mousePoint:function(x,y){
this.x=x;
this.y=y;
},
mousePosition:function (evnt){
evnt=evnt||window.event;
var x=parseInt(evnt.clientX);
var y=parseInt(evnt.clientY) ;
return new Drag.mousePoint(x,y);
},
getMouseOffset:function(target,evnt){
var mousePos=Drag.mousePosition(evnt);
var x =mousePos.x-target.offsetLeft;
var y=mousePos.y-target.offsetTop;
return new Drag.mousePoint(x,y);
},
mouseUp:function (evnt ){
Drag.dragObject=null;
document.onmousemove = null;
document.onmouseup = null;
},
mouseMove:function(evnt){
if(! Drag.dragObject) return;
var mousePos=Drag.mousePosition(evnt);
Drag.dragObject.style.position="absolute";
Drag.dragObject.style.top=mousePos.y-Drag .mouseOffset.y "px";
Drag.dragObject.style.left=mousePos.x-Drag.mouseOffset.x "px";
return false;
}
}

Since the code is not very complicated, I won’t go into details. If necessary, you can directly download and use it. After importing, use the Drag.initDrag (id name) method to apply it.
In fact, there is already a lightweight dom-drag class library on the Internet (author: Aaron Boodman). When I look back at the class library I wrote, I am full of confidence... The dom-drag class can be found at http:/ /boring.youngpup.net/projects/dom-drag/Download, the code is very simple and provides many easy-to-use features. And very detailed examples are provided on the website.
This class library is like the startDrag method in flash, with streamlined code but practical functions. If you want a component to be draggable, you can call the Drag.init() method.
init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
o: It is a component that wants to realize mouse dragging;
oRoot: It is the upper layer of o Component will be dragged along with o;
minX, maxX, minY, maxY: Set the range of mouse dragging. As can be seen from the example cropper below, if there is oRoot, the range is determined by oRoot relative to the page. ;
bSwapHorzRef, bSwapVertRef: Set the priority of the component;
fXMapper, fYMapper: Set the drag path

The third example:
Example link: http://boring .youngpup.net/projects/dom-drag/ex3.html
You can limit the drag range, the code is as follows:
Copy code The code is as follows:




< head>