Js mouse drag object:
Copy codeThe Code is as follows:
// Define the mouse drag object
Drag = function (a, o ){
Var d = document; if (! A) a = window. event;
If (! A. pageX) a. pageX = a. clientX;
If (! A. pageY) a. pageY = a. clientY;
Var x = a. pageX, y = a. pageY;
If (o. setCapture)
O. setCapture ();
Else if (window. captureEvents)
Window. captureEvents (Event. MOUSEMOVE | Event. MOUSEUP );
Var backData = {x: o. style. top, y: o. style. left };
D. onmousemove = function (){
If (! A) a = window. event;
If (! A. pageX) a. pageX = a. clientX;
If (! A. pageY) a. pageY = a. clientY;
Var tx = a. pageX-x + parseInt (o. style. left), ty = a. pageY-y + parseInt (o. style. top );
O. style. left = tx + "px ";
O. style. top = ty + "px ";
X = a. pageX;
Y = a. pageY;
};
D. onmouseup = function (){
If (! A) a = window. event;
If (o. releaseCapture)
O. releaseCapture ();
Else if (window. captureEvents)
Window. captureEvents (Event. MOUSEMOVE | Event. MOUSEUP );
D. onmousemove = null;
D. onmouseup = null;
If (! A. pageX) a. pageX = a. clientX;
If (! A. pageY) a. pageY = a. clientY;
If (! Document. body. pageWidth) document. body. pageWidth = document. body. clientWidth;
If (! Document. body. pageHeight) document. body. pageHeight = document. body. clientHeight;
If (a. pageX <1 | a. pageY <1 | a. pageX> document. body. pageWidth | a. pageY> document. body. pageHeight ){
O. style. left = backData. y;
O. style. top = backData. x;
}
};
}
Usage:
Copy codeThe Code is as follows:
<Div id = "divPhoto" onmousedown = "drag (event, this)"> </div>