方块拖动

  <!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>方块拖动</title>
  <style>
  #test{
  background-color: purple;
  width: 100px;
  height: 100px;
  position: absolute;
  top: 500px;
  left: 500px;
  }
  </style>
  </head>
  <body>
  <div id="test" draggable="true">
   
  </div>
  <script>
  window.onload=function(){
  let drag =document.getElementById('test');
  drag.ondragstart=function(e){
  e=e || window.event;
  let x=e.offsetX;
  let y=e.offsetY;
  document.ondragover=function(e){
  e.preventDefault();
  }
  document.ondrop=function(e){
  drag.style.left=e.clientX-x+"px";
  drag.style.top=e.clientY-y+"px";
  }
  }
  }
  </script>
  </body>
  </html>
posted @ 2019-09-12 16:47  七*月  阅读(101)  评论(0编辑  收藏  举报