div移动层精简示例(event事件兼容多浏览器)

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title> New Document </title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta http-equiv="Content-Language" content="zh-CN" />
  7. <script language="JavaScript" type="text/javascript">
  8. var drag_=false
  9. var D=new Function('obj','return document.getElementById(obj);')
  10. var oevent=new Function('e','if (!e) e = window.event;return e')
  11. function Move_obj(obj){
  12.     var x,y;
  13.     D(obj).onmousedown=function(e){
  14.         drag_=true;
  15.         with(this){
  16.             style.position="absolute";var temp1=offsetLeft;var temp2=offsetTop;
  17.             x=oevent(e).clientX;y=oevent(e).clientY;
  18.             document.onmousemove=function(e){
  19.                 if(!drag_)return false;
  20.                 with(this){
  21.                     style.left=temp1+oevent(e).clientX-x+"px";
  22.                     style.top=temp2+oevent(e).clientY-y+"px";
  23.                 }
  24.             }
  25.         }
  26.         document.onmouseup=new Function("drag_=false");
  27.     }
  28. }
  29. </script>
  30. <body>
  31. <div id="test" style="background-color:#0099CC;width:200px;height:100px;border:1px #000 solid;" onmouseover='Move_obj("test")'>鼠标过来点击拖动层</div>
  32. </body>
  33. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值