zoukankan      html  css  js  c++  java
  • JavaScript 面向对象的拖拽

    一、body

    <div id="box"></div>

    二、css

    <style>

    #box {

      position: abaolute;

      top: 0;

      left: 0;

       100px;

      height: 100px;

    }

    </style>

    三、JavaScript

    <script>

      class Drag{

        constructor(el) {

          this.el = el;

          el.startOffset = null;

          el.startPoint = null;

          let move = (e) => {

            this.move(e);

          }

          let end = (e)=>{

            document.removeEventListener('mousemove', move);

            document.removeEventListener('mouseup', end);

          }

          el.addEventListener('mousedown', (e)=>{

            this.start(e);

            document.addEventListener('mousemove', move);

            document.addEventListener('mouseup', end);

          })

        }

        start(e) {

          let {el} = this;

          this.startOffset = {

            x: el.offsetLeft,

            y: el.offsetTop

          }

          this.startPoint = {

            x: e.clientX,

            y: e.clientY

          }

        }

        move(e) {

          let {el, startOffset, startPoint} = this;

          let nowPoint = {

            x: e.clientX,

            y: e.clientY

          }

          let dis = {

            x: nowPoint.x - startPoint.x,

            y: nowPoint.y - startPoint.y

          }

          el.style.left = startOffset.x + dis.x + 'px';

          el.style.top =startOffset.y + dis.y + 'px';

        }

      }

      let box = document.querySelector('#box');

      let drag = new Drag(box);

    </script>

  • 相关阅读:
    redis.conf 参数说明
    BD2 catalog编目
    Altium Designer -在PCB中统一修改元器件标号的位置
    AD使用技巧-怎样绘制圆形敷铜
    运放压百率
    Altium designer 新建快捷键
    修改AD中PCB各层的透明度
    Altium Designer 使用小技巧2
    Altium Designer 使用中的小技巧1
    网络库的设计与实现
  • 原文地址:https://www.cnblogs.com/hyshi/p/10913552.html
Copyright © 2011-2022 走看看