用JavaScript移动对象

                             drag me->  <-drag me

源代码:

< script  type ="text/javascript" >
var xOffset,yOffset;
function dragStart()
{
    
var obj=event.srcElement;
    obj.style.position
="absolute";
    obj.setCapture();
    xOffset
=event.x-obj.offsetLeft;
    yOffset
=event.y-obj.offsetTop;
}

function doDrag()
{
    event.srcElement.style.left
=event.x-xOffset;
    event.srcElement.style.top 
=event.y-yOffset;
}

function dragEnd()
{
    event.srcElement.style.cursor
="default";
    event.srcElement.releaseCapture();
}

</ script >
< img  src ="http://www.w3.org/Icons/valid-xhtml10.png"  onmousedown ="this.style.cursor='move'"  onmouseup ="this.style.cursor='default'"  ondragstart ="dragStart()"  ondrag ="doDrag()"  ondragend ="dragEnd()"   />

你可能感兴趣的:(JavaScript,function)