您的位置:首页 > Web前端 > JavaScript

JS 鼠标拖动效果实现

2012-05-09 15:50 567 查看
相对于鼠标拖动面板跟随变动效果,废话不多说了,具体代码:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>鼠标拖动</title> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <scriptlanguage="JavaScript"type="text/javascript"> vardrag_=false varD=newFunction('obj','returndocument.getElementById(obj);') varoevent=newFunction('e','if(!e)e=window.event;returne') functionMove_obj(obj){ varx,y; D(obj).onmousedown=function(e){ drag_=true; with(this){ style.position="absolute";vartemp1=offsetLeft;vartemp2=offsetTop; x=oevent(e).clientX;y=oevent(e).clientY; document.onmousemove=function(e){ if(!drag_)returnfalse; with(this){ style.left=temp1+oevent(e).clientX-x+"px"; style.top=temp2+oevent(e).clientY-y+"px"; } } } document.onmouseup=newFunction("drag_=false"); } } </script> <body> <divid="drag"style="background-color:#0066CC;width:280px;height:160px;padding:20px;border:1px#003399solid;font-size:10.5pt;color:white"onmouseover='Move_obj("drag")'> <p>这个层是可以拖动的,而且还可以吸附鼠标,试试看!</p> <p>/</p> </div> </body> </html>   

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: