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>

  • 相关阅读:
    (转)机器学习——深度学习(Deep Learning)
    (转)Deep Learning深度学习相关入门文章汇摘
    (转)Haar-like矩形遍历检测窗口演示Matlab源代码
    HTML5远程工具
    splinter操作ie浏览器
    Wechat login authorization(OAuth2.0)
    Message Queuing(MSMQ)
    Visual Studio2017 数据库架构比较
    Visual Studio2017 Remote Debugger
    搭建Spring Initializr服务器
  • 原文地址:https://www.cnblogs.com/hyshi/p/10913552.html
Copyright © 2011-2022 走看看