Vue implements image drag and drop function

Vue implements image drag and drop function

This article example shares the specific code of Vue to realize the image drag and drop function for your reference. The specific content is as follows

1. Mainly involved element knowledge, schematic diagram:

2. js code part:

directives: {
    drag: {
      // Definition of instruction bind: function(el) {
        // Get the current element let oDiv = el;
        oDiv.onmousedown = (e) => {
          // Calculate the position of the mouse relative to the element let disX = e.clientX - oDiv.offsetLeft;
          let disY = e.clientY - oDiv.offsetTop;

          document.onmousemove = (e) => {
            // Subtract the position of the mouse relative to the element from the position of the mouse to get the position of the element let left = e.clientX - disX;
            let top = e.clientY - disY;

            oDiv.style.left = left + 'px';
            oDiv.style.top = top + 'px';
          };
          document.onmouseup = () => {
            document.onmousemove = null;
            document.onmouseup = null;
          }
        }
      }
    }
    }

3. Usage:

<div class="card" v-drag id="card">
<img src="../assets/logo.png" >
</div>

4. Style part (position must be set to absolute):

.card {
  position: absolute;
  float: left;
  width: 200px;
  height: 200px;

}

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:
  • Vue integrates a rich text editor that supports image zooming and dragging
  • Vue implements image preview effect example (zoom in, zoom out, drag)
  • Vue implements drag and drop or click to upload pictures
  • Vue implements the problem of image scaling
  • Use vue components to realize the drag and zoom function of pictures

<<:  The One-Hand Rule of WEB2.0

>>:  Use and optimization of MySQL COUNT function

Recommend

React Fragment Introduction and Detailed Usage

Table of contents Preface Motivation for Fragment...

Summary of 7 reasons why Docker is not suitable for deploying databases

Docker has been very popular in the past two year...

Do not start CSS pseudo-class names with numbers

When newbies develop div+css, they need to name t...

What is em? Introduction and conversion method of em and px

What is em? em refers to the font height, and the ...

Using JS timer to move elements

Use JS timer to make an element to make a method ...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

Linux firewall iptables detailed introduction, configuration method and case

1.1 Introduction to iptables firewall Netfilter/I...

JS implements random generation of verification code

This article example shares the specific code of ...

How to restore a database and a table from a MySQL full database backup

In the official MySQL dump tool, how can I restor...

MySQL slow query operation example analysis [enable, test, confirm, etc.]

This article describes the MySQL slow query opera...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Detailed explanation of Nodejs array queue and forEach application

This article mainly records the problems and solu...