日期:2014-05-16 浏览次数:20363 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> window.onload=function(){ var oA=document.getElementById('a'); var oT=document.getElementById('t'); oT.onmousedown=function(e){ var oTleft=oT.offsetLeft; var oTtop =oT.offsetTop; var Event=e||event; var Mx=Event.offsetX;//保存鼠标的横坐标 var My=Event.offsetY;//保存鼠标的纵坐标 var disX=Mx-oTleft;//当鼠标按下时,计算鼠标相对于子模块的横坐标 var disY=My-oTtop;//当鼠标按下时,计算鼠标相对于子模块的纵坐标 oA.onmousemove=function(e){ var Event=e||event; var tl=Event.offsetX-disX; var tt=Event.offsetY-disY; oT.style.left=tl+'px'; oT.style.top=tt+'px'; return false; } oA.onmouseup=function(e){ oA.onmousemove=null; oA.onmouseup=null; } return false; } }; </script> </head> <body> <div id="a" style=" background-color: #bf0000; height: 200px; position:relative;"> 动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动拖动 <div id="t" style="width:50px; height:50px; background-color:#fff; position:absolute; top:0px; left:0px;"> <span>拖动</span> </div> </div> </body> </html>
oT.onmousemove = function(){ if ( e && e.stopPropagation ) //因此它支持W3C的stopPropagation()方法 e.stopPropagation(); else //否则,我们需要使用IE的方式来取消事件冒泡 window.event.cancelBubble = true; } }