Detailed explanation of the use of Vue image drag and drop zoom component

Detailed explanation of the use of Vue image drag and drop zoom component

The specific usage of the Vue image drag and drop zoom component is for your reference. The specific content is as follows

<doc>
  Image component - user zooms in and out and drags</doc>
<template>
  <div style="width: 100%;position: relative;overflow: hidden;text-align: center;border: 1px solid #f1f2f3;">
    <el-button size='mini' @click="toBIgChange" icon="el-icon-zoom-in"
      style="position: absolute;top: 2px ;left: 2px;z-index: 999;"></el-button>
    <el-button size='mini' @click="toSmallChange" icon="el-icon-zoom-out"
      style="position: absolute;top: 2px ;left: 40px;z-index: 999;"></el-button>
    <img id="img" :src="src" alt="" @mousedown.prevent="dropImage" :style="{transform:'scale('+multiples+')'}">
  </div>
</template>
<script>
  export default {
    props: ['src'],
    data() {
      return {
        multiples: 1,
        odiv: null,
      }
    },
    mounted() {
      this.dropImage()
    },
    watch:
      src(newValue, oldValue) {
        this.multiples = 1
        if (this.odiv !== null) {
          this.odiv.style.left = '0px';
          this.odiv.style.top = '0px';
        }
      },
    },
    methods: {
      toBIgChange() {
        if (this.multiples >= 2) {
          return;
        }
        this.multiples += 0.25;
      },
      //Shrink toSmallChange() {
        if (this.multiples <= 1) {
          return;
        }
        this.multiples -= 0.25;
      },
      // drag dropImage(e) {
        if (e === null) {
          return
        }
        this.odiv = e.target; //Get the target element //Calculate the position of the mouse relative to the element let disX = e.clientX - this.odiv.offsetLeft;
        let disY = e.clientY - this.odiv.offsetTop;
        document.onmousemove = (e) => { //Mouse pressed and moved event //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;

          //Bind the element position to positionX and positionY this.positionX = top;
          this.positionY = left;

          //Move the current element this.odiv.style.left = left + 'px';
          this.odiv.style.top = top + 'px';
        };
        document.onmouseup = (e) => {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      },
    }
  }
</script>
<style scoped>
  img {
    width: 100%;
    position: relative;
  }
</style> 

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:
  • Use vue components to realize the drag and zoom function of pictures
  • Vue implements image preview effect example (zoom in, zoom out, drag)
  • Vue implements zoom in, zoom out and drag function
  • Implementing Vue image scaling - dragging components

<<:  How to set the default value of a MySQL field

>>:  Detailed explanation of the deployment process of SEATA transaction service Docker

Recommend

WeChat applet date and time component (year, month, day, hour, and minute)

This article example shares the specific code of ...

The main idea of ​​​​dynamically setting routing permissions in Vue

I have seen some dynamic routing settings on the ...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

Some small methods commonly used in html pages

Add in the <Head> tag <meta http-equiv=&q...

Perfect solution for JavaScript front-end timeout asynchronous operation

Table of contents What happens if a piece of code...

Teach you how to use MySQL8 recursive method

I have previously written an article about recurs...

A brief discussion on the fun of :focus-within in CSS

I believe some people have seen this picture of c...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

Common shell script commands and related knowledge under Linux

Table of contents 1. Some points to remember 1. V...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

Web design must also first have a comprehensive image positioning of the website

⑴ Content determines form. First enrich the conten...

Three examples of blur background effects using CSS3

Let’s not start with the introduction and get str...

How to disable the automatic password saving prompt function of Chrome browser

Note: In web development, after adding autocomplet...

12 Useful Array Tricks in JavaScript

Table of contents Array deduplication 1. from() s...