javascript拖拽事件

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        #a{
            width: 100px;
            height: 100px;
            background: yellow;
            position: absolute; 
        }
    </style>
</head>
<body>
  <div id='a'></div>
  <script type="text/javascript">
      a.onmousedown=function(e){
          var ev=e||event
          var l=ev.clientX -this.offsetLeft;
          var t=ev.clientY -this.offsetTop;
          document.onmousemove=function(e){
           var ev=e||event
            a.style.left=ev.clientX-l+'px';
            a.style.top=ev.clientY-t+'px'
          };
          document.onmouseup=function(){
              document.onmousemove=null;
              document.onmousedown=null;
      
          }
return false; }
</script> </body> </html>

 

posted on 2019-03-28 21:48  manong!!  阅读(173)  评论(0编辑  收藏  举报

导航