Note: Use preventDefault to prevent default browser event operations.
<Script language = "javascript" type = "text/javascript">
Var isDrag = false;
Function isIE (){
If (navigator. userAgent. indexOf ("MSIE")> 0) {return true ;}
Else {return false ;}
}
Function addListener (element, e, fn ){
If (isIE () {element. attachEvent ("on" + e, fn );}
Else {element. addEventListener (e, fn, false );}
}
Function drag (e ){
Var e = e | window. event;
Var element = e. srcElement | e.tar get;
If (e. preventDefault) e. preventDefault ();
Else e. returnvalue = false;
IsDrag = true;
Var relLeft = e. clientX-parseInt (element. style. left );
Var relTop = e. clientY-parseInt (element. style. top );
Element. onmouseup = function () {isDrag = false ;}
Document. onmousemove = function (e_move ){
Var e_move = e_move | window. event;
If (isDrag ){
Element. style. left = e_move.clientX-relLeft + "px ";
Element. style. top = e_move.clientY-relTop + "px ";
Return false;
}
}
}
Window. onload = function ()
{
Var element = document. getElementById ("elimg ");
Var element2 = document. getElementById ("eldiv ");
AddListener (element, "mousedown", drag );
AddListener (element2, "mousedown", drag );
}
</Script>
<Div id = "eldiv" style = "width: 140px; height: 100px; background: # EEE; position: absolute; left: 117px; top: 124px"> </div>