Vue implements zoom in, zoom out and drag function

Vue implements zoom in, zoom out and drag function

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

Click to zoom in to full screen and click again to zoom out to original size. This pop-up is based on element dialog.

1. Create a new directives.js under the utils folder (it is packaged and can be used directly)

import Vue from 'vue';
import bigPic from '../assets/images/bigChange.png';
// v-dialogDrag: pop-up window drag attribute Vue.directive('dialogDrag', {
  bind(el, binding, vnode, oldVnode) {
    //The minimum width and height of the bullet box let minWidth = 400; 
    let minHeight = 300; 
    //Initial non-full screen let isFullScreen = false; 
    //Current width and height let nowWidth = 0; 
    let nowHight = 0; 
    //Current top height let nowMarginTop = 0; 
    //Get the header of the pop-up box (this part can be double-clicked to full screen) 
    const dialogHeaderEl = el.querySelector('.el-dialog__header'); 
    //Pop-up window const dragDom = el.querySelector('.el-dialog'); 
    // Full screen button dialogHeaderEl.insertAdjacentHTML('beforeEnd', '<div class="bigbtn"><div class="changeBig"></div><div class="changeSmall"></div></div>'); 
    const bigBtn = el.querySelector('.bigbtn'); 
    const changeBig = el.querySelector('.changeBig'); 
    const changeSmall = el.querySelector('.changeSmall');
    //Add overflow auto to the pop-up window; otherwise the label in the box may exceed the dialog when it is reduced; 
    dragDom.style.overflow = "auto"; 
    //Clear the selected header text effect//dialogHeaderEl.onselectstart = new Function("return false"); 
    //Add a draggable cursor to the head 
    dialogHeaderEl.style.cursor = 'move'; 
    // Get the original attributes ie dom element.currentStyle firefox google window.getComputedStyle(dom element, null); 
    const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); 
    let moveDown = (e) => {
      // Press the mouse to calculate the distance between the current element and the visible area const disX = e.clientX - dialogHeaderEl.offsetLeft; 
      const disY = e.clientY - dialogHeaderEl.offsetTop; 
      // Get the value with px regular expression to replace let styL, styT;
      // Note that in IE, the first value obtained is the component's own 50% and the value is assigned to px after moving 
      if (sty.left.includes('%')) {
        styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100); 
        styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);
 
      } else { 
        styL = +sty.left.replace(/\px/g, '');
        styT = +sty.top.replace(/\px/g, '');
 
      };
 
      document.onmousemove = function (e) {
        // Calculate the moving distance through event delegation const l = e.clientX - disX;
        const t = e.clientY - disY; 
        // Move the current element dragDom.style.left = `${l + styL}px`;
        dragDom.style.top = `${t + styT}px`;
 
        //Pass the current position //binding.value({x:e.pageX,y:e.pageY})
 
      };
 
      document.onmouseup = function (e) { 
        document.onmousemove = null; 
        document.onmouseup = null;
 
      };
 
    }
 
    dialogHeaderEl.onmousedown = moveDown;
 
    //Double click the header to achieve full screen effect bigBtn.onclick = (e) => {
      if (isFullScreen == false) { 
        changeBig.style.display = 'none'; 
        changeSmall.style.display = 'block';
        nowHight = dragDom.clientHeight;
        nowWidth = dragDom.clientWidth; 
        nowMarginTop = dragDom.style.marginTop; 
        dragDom.style.left = 0; 
        dragDom.style.top = 0; 
        dragDom.style.height = "100VH";
        dragDom.style.width = "100VW"; 
        dragDom.style.marginTop = 0; 
        isFullScreen = true; 
        dialogHeaderEl.style.cursor = 'initial'; 
        dialogHeaderEl.onmousedown = null;
 
      } else {
 
        changeBig.style.display = 'block'; 
        changeSmall.style.display = 'none';
        dragDom.style.height = "auto"; 
        dragDom.style.width = nowWidth + 'px'; 
        dragDom.style.marginTop = nowMarginTop; 
        isFullScreen = false; 
        dialogHeaderEl.style.cursor = 'move';
 
        dialogHeaderEl.onmousedown = moveDown;
 
      }
 
    }
 
    dragDom.onmousemove = function (e) {
      // console.log(dialogHeaderEl,dragDom.querySelector('.el-dialog__body'), 123);
      let moveE = e;
 
      if (e.clientX > dragDom.offsetLeft + dragDom.clientWidth - 10 || dragDom.offsetLeft + 10 > e.clientX) {
         dragDom.style.cursor = 'w-resize';
      } else if (el.scrollTop + e.clientY > dragDom.offsetTop + dragDom.clientHeight - 10) { 
        dragDom.style.cursor = 's-resize'; 
      } else { 
        dragDom.style.cursor = 'default'; 
        dragDom.onmousedown = null;
 
      }
 
      dragDom.onmousedown = (e) => { 
        const clientX = e.clientX; 
        const clientY = e.clientY; 
        let elW = dragDom.clientWidth; 
        let elH = dragDom.clientHeight; 
        let EloffsetLeft = dragDom.offsetLeft;
        let EloffsetTop = dragDom.offsetTop;
        dragDom.style.userSelect = 'none';
        let ELscrollTop = el.scrollTop;
 
        //Judge whether the clicked position is the headif (clientX > EloffsetLeft && clientX < EloffsetLeft + elW && clientY > EloffsetTop && clientY < EloffsetTop + 100) {
 
          //If it is the header, no action will be taken here. The above is bound to dialogHeaderEl.onmousedown = moveDown;
 
        } else {
 
          document.onmousemove = function (e) {
            e.preventDefault(); // Disable default events when moving // Left mouse drag position if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) { 
              //Drag to the left if (clientX > e.clientX) { 
                dragDom.style.width = elW + (clientX - e.clientX) * 2 + 'px';
 
              }
 
              //Drag to the right if (clientX < e.clientX) { 
                if (dragDom.clientWidth < minWidth) {
                } else {
                dragDom.style.width = elW - (e.clientX - clientX) * 2 + 'px';
 
                }
 
              }
 
            }
 
            //Right mouse drag positionif (clientX > EloffsetLeft + elW - 10 && clientX < EloffsetLeft + elW) { 
              //Drag to the left if (clientX > e.clientX) {
                if (dragDom.clientWidth < minWidth) { 
                } else { 
                  dragDom.style.width = elW - (clientX - e.clientX) * 2 + 'px';
 
                }
 
              }
 
              //Drag to the right if (clientX < e.clientX) {
 
                dragDom.style.width = elW + (e.clientX - clientX) * 2 + 'px';
 
              }
 
            }
 
            //Bottom mouse drag positionif (ELscrollTop + clientY > EloffsetTop + elH - 20 && ELscrollTop + clientY < EloffsetTop + elH) {
 
              //Drag up if (clientY > e.clientY) {
                if (dragDom.clientHeight < minHeight) {
                } else {
 
                  dragDom.style.height = elH - (clientY - e.clientY) * 2 + 'px';
 
                }
 
              }
 
              //Drag down if (clientY < e.clientY) { 
                dragDom.style.height = elH + (e.clientY - clientY) * 2 + 'px';
 
              }
 
            }
 
          };
 
          //Stretching ends document.onmouseup = function (e) { 
            document.onmousemove = null; 
            document.onmouseup = null;
 
          };
 
        }
 
      }
 
    }
  }
})

2. Import it into main.js

import './utils/directives.js';

3. The page is used directly with a custom attribute name ( v-dialogDrag )

<el-dialog
     v-dialogDrag
      title="User Information"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      :before-close="handleClose">
      <div>
        <el-table :data="phoneData" style="width: 100%">
          <el-table-column prop="fullName" label="Name" width="100"></el-table-column>
          <el-table-column prop="sex" label="Gender" width="80"></el-table-column>
          <el-table-column prop="idCard" label="ID number" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="mobile" label="Contact number" width="150"></el-table-column>
          <el-table-column prop="censusAreaName" label="Household Registration Division" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="censusAddr" label="Household Registration Address" min-width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="operation" width="120" fixed="right" align="center">
            <template slot-scope="{row}">
              <span @click="chooseInfor(row.id)" class="table-btn">Select</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
</el-dialog>

4. CSS is also provided (pay attention to the label elements in directives.js)

.changeBig {
    width: 20px;
    height: 20px;
    background: url("../assets/images/bigChange.png") no-repeat center;
    background-size: 100% 100%;
    cursor: pointer;
    position: absolute;
    top: 14px;
    right: 60px;
}
.changeSmall {
    width: 20px;
    height: 20px;
    background: url("../assets/images/smallChange.png") no-repeat center;
    background-size: 100% 100%;
    cursor: pointer;
    position: absolute;
    top: 14px;
    right: 60px;
    display: none;
}

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)
  • Detailed explanation of the use of Vue image drag and drop zoom component
  • Implementing Vue image scaling - dragging components

<<:  Introduction to Linux and the most commonly used commands (easy to learn, but can solve more than 95% of the problems)

>>:  Ubuntu16.04 installation mysql5.7.22 graphic tutorial

Recommend

What are the differences between var let const in JavaScript

Table of contents 1. Repeated declaration 1.1 var...

CentOS 8.0.1905 installs ZABBIX 4.4 version (verified)

Zabbix Server Environment Platform Version: ZABBI...

Docker packages the local image and restores it to other machines

1. Use docker images to view all the image files ...

Configure VIM as a C++ development editor in Ubuntu

1. Copy the configuration file to the user enviro...

Web Design Tutorial (5): Web Visual Design

<br />Previous article: Web Design Tutorial ...

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

In-depth understanding of JavaScript event execution mechanism

Table of contents Preface The principle of browse...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

DD DT DL tag usage examples

We usually use the <ul><li> tags, but ...

React+Koa example of implementing file upload

Table of contents background Server Dependencies ...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

How to build pptpd service in Alibaba Cloud Ubuntu 16.04

1. To build a PPTP VPN, you need to open port 172...

Disabled values ​​that cannot be entered cannot be passed to the action layer

If I want to make the form non-input-capable, I se...

MySQL concurrency control principle knowledge points

Mysql is a mainstream open source relational data...