Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="description" content="一个面向过程的拖拽demo">
  <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>Document</title>
  <style>
    #drag{
      width: 40px;
      height: 40px;
      position: absolute;
      background-color: red;
      cursor:pointer;;
    }
  </style>
</head>
<body>
  <div id="drag">
  </div>
</body>
<script>
  var drag = document.getElementById("drag");
  drag.onmousedown = function(ev){
    var ev = ev || window.event;
    var x = ev.clientX-this.offsetLeft;
    var y = ev.clientY-this.offsetTop;
    document.onmousemove = function(ev){
      var ev = ev || window.event;
      var nowx= ev.clientX-x;
      var nowy= ev.clientY-y;
      drag.style.left = nowx + 'px';
      drag.style.top = nowy + 'px';
    }
    document.onmouseup = function(){
      document.onmousemove = null;
      document.onmouseup = null;
    }
  }
</script>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
buxukupro
0viewers