js object-oriented method to achieve drag effect

js object-oriented method to achieve drag effect

This article shares the specific code for implementing drag and drop in js object-oriented way for your reference. The specific content is as follows

The implementation principle of the drag function: (take it away directly!)

<!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>Document</title>
  <style>
    #box {
      position: absolute;
      left: 100px;
      top: 100px;
      width: 100px;
      height: 100px;
      background: red;
    }

    #box2 {
      position: absolute;
      left: 200px;
      top: 200px;
      width: 100px;
      height: 100px;
      background: green;
    }
  </style>
</head>

<body>
  <div id="box">Text</div>
  <div id="box2">Text</div>
</body>
<script>
  class Drag {
    startMouse = {};
    startEl = {};
    #el = null;
    constructor(el, option) {
      this.#el = el;
      this.option = option;
      this.start();
    }
    start() {
      let move = (e) => {
        this.move(e)
      }
      this.#el.addEventListener('mousedown', (e) => {
        this.startMouse = {
          x: e.clientX,
          y: e.clientY,
        }
        this.ondragstart && this.ondragstart(e)
        this.startEl = this.getOffset();
        document.addEventListener('mousemove', move);
        document.addEventListener('mouseup', (e) => {
          document.removeEventListener('mousemove', move);
          this.end(e);
        }, {
          once: true
        })
        e.preventDefault();

      })
    }
    move(e) {
      let nowMouse = {
        x: e.clientX,
        y: e.clientY,
      }
      let disMouse = {
        x: nowMouse.x - this.startMouse.x,
        y: nowMouse.y - this.startMouse.y
      }
      this.ondrag && this.ondrag(e)
      this.setOffset(disMouse)
    }
    end(e) {
      this.ondragend && this.ondragend(e)
    }
    getOffset() {
      return {
        x: parseFloat(getComputedStyle(this.#el)["left"]),
        y: parseFloat(getComputedStyle(this.#el)["top"])
      }
    }
    setOffset(dis) {
      this.#el.style.left = this.startEl.x + dis.x + 'px'
      this.#el.style.top = this.startEl.y + dis.y + 'px'
    }
  }
  let box = document.querySelector("#box");
  let box2 = document.querySelector("#box2");
  let d = new Drag(box);
  let d2 = new Drag(box2);
  let clonex = null;
  d2.ondragstart = (e) => {
    clonex = box2.cloneNode(true);
    document.body.appendChild(clonex)
    box2.style.opacity = 0.5
  }
  d2.ondragend = () => {
    document.body.removeChild(clonex);
    box2.style.opacity = 1
  }
</script>

</html>

The final effect (the dragged block is the green block)

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Let's take a look at object-oriented programming in javascript
  • Detailed explanation of JavaScript object-oriented practice: encapsulation and dragging objects
  • Detailed explanation of JavaScript object-oriented programming [class creation, instance objects, constructors, prototypes, etc.]
  • Summary of JavaScript object-oriented core knowledge and concepts
  • Detailed examples of the seven basic principles of JavaScript object-oriented
  • Do you know JavaScript object-oriented?

<<:  Solution to the same IP after cloning Ubuntu 18 virtual machine

>>:  How to change the mysql password on the Xampp server (with pictures)

Recommend

Detailed explanation of 5 solutions for CSS intermediate adaptive layout

Preface When making a page, we often encounter co...

VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram

1. Create a new virtual machine from VMware 15.5 ...

Linux parted disk partition implementation steps analysis

Compared with fdisk, parted is less used and is m...

JDBC Exploration SQLException Analysis

1. Overview of SQLException When an error occurs ...

Several common methods of CSS equal height layout

Equal height layout Refers to the layout of child...

MySQL green version setting code and 1067 error details

MySQL green version setting code, and 1067 error ...

The most comprehensive collection of front-end interview questions

HTML+CSS 1. Understanding and knowledge of WEB st...

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

Better-scroll realizes the effect of linking menu and content

1. Basic use <!DOCTYPE html> <html lang=...

Start nginxssl configuration based on docker

Prerequisites A cloud server (centOS of Alibaba C...

mysql5.7.17.msi installation graphic tutorial

mysql-5.7.17.msi installation, follow the screens...

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but...

Learn the key knowledge that must be mastered in the Vue framework

1. What is Vue Vue is a progressive framework for...