admin 发表于 2020-12-20 09:11:46

ES6_拖拽

<div id="box"></div>
CSS:
div {
      width:100px;
      height:100px;
      border-radius:50%;
      position:absolute;
}
JS:
class Pen {
    constructor() {
      document.onmousedown = this.down.bind(this);
    }
    down() {
      document.onmousemove = this.move.bind(this);
      document.onmouseup = this.up.bind(this);
    }
    move(e) {
      var e = e || event;
      var div = document.getElementById("box");
      div.style.cssText = "left:" + e.clientX + "px; top:" + e.clientY + "px;";
      div.style.background = "blue";
    }
    up() {
      document.onmousemove = null;
      document.onmouseup = null;
    }
}

new Pen();

页: [1]
查看完整版本: ES6_拖拽