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 steps to install Hadoop cluster under Linux

Table of contents 1. Create a Hadoop directory in...

How to use ECharts in WeChat Mini Programs using uniapp

Today, we use uniapp to integrate Echarts to disp...

In-depth explanation of nginx location priority

location expression type ~ indicates to perform a...

Detailed example of MySQL data storage process parameters

There are three types of MySQL stored procedure p...

Writing High-Quality Code Web Front-End Development Practice Book Excerpts

(P4) Web standards are composed of a series of sta...

How to deploy nodejs service using Dockerfile

Initialize Dockerfile Assuming our project is nam...

JS uses map to integrate double arrays

Table of contents Preface Simulating data Merged ...

Detailed explanation of the sticky position attribute in CSS

When developing mobile apps, you often encounter ...

Tips for importing csv, excel or sql files into MySQL

1. Import csv file Use the following command: 1.m...

Let's talk about the difference between MyISAM and InnoDB

The main differences are as follows: 1. MySQL use...

Prometheus monitors MySQL using grafana display

Table of contents Prometheus monitors MySQL throu...

uniapp dynamic modification of element node style detailed explanation

Table of contents 1. Modify by binding the style ...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

Summary of the application of transition components in Vue projects

​Transtion in vue is an animation transition enca...