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

Detailed explanation of how to use Vue to load weather components

This article shares with you how to use Vue to lo...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

Detailed tutorial on installing ElasticSearch 6.4.1 on CentOS7

1. Download the ElasticSearch 6.4.1 installation ...

HTML table layout example explanation

The elements in an HTML document are arranged one...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...

Tutorial on upgrading from Centos7 to Centos8 (with pictures and text)

If you upgrade in a formal environment, please ba...

Causes and solutions for cross-domain issues in Ajax requests

Table of contents 1. How is cross-domain formed? ...

A brief analysis of the responsiveness principle and differences of Vue2.0/3.0

Preface Since vue3.0 was officially launched, man...

JavaScript anti-shake and throttling explained

Table of contents Stabilization Throttling Summar...

Recommended 20 best free English handwriting fonts

Jellyka BeesAntique Handwriting [ank]* Jellyka Cut...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Stop using absolute equality operators everywhere in JS

Table of contents Overview 1. Test for null value...

Some points on using standard HTML codes in web page creation

The most common mistake made by many website desi...

Detailed explanation of HTML document types

Mine is: <!DOCTYPE html> Blog Garden: <!...