Detailed explanation of the encapsulation and use of the Vue image magnifier component

Detailed explanation of the encapsulation and use of the Vue image magnifier component

Based on the Vue image magnifier component package, for your reference, the specific content is as follows

The implementation process of the picture magnifier is to place a small picture in a box. When the mouse moves in the small picture box, a moving block (shadow area/mask layer) appears, and a proportionally enlarged picture content in the moving block of the small picture box appears in the large picture box on the right. The effect diagram is as follows:

The Vue component code for implementing the image magnifying glass effect is as follows:

<template>
  <div>
    <div class="move" @mouseover="over()" @mouseout="out()" @mousemove="move($event)">
      <div id="small"> //Small picture and mask layer container<div id="float"></div> //Mask layer<img :src="item" id="smallimg"> //Picture to be enlarged</div>
    </div>
    <div id="big">
      <img :src="item"> //Enlarged image</div>
  </div>
</template>

<script>
  var float,smallimg,big,bigImg,small,float_maxJL_t,float_maxJL_l,bigImg_maxJL_t,bigImg_maxJL_l;

  export default{
    props: {
      item:
        type: String
      }
    },
    data() {
     return {
     }
    }, 

    mounted(){
      float = document.getElementById("float"); //Left mask layer smallimg = document.getElementById("smallimg"); //Small image on the left big = document.getElementById("big"); //Right visible area bigImg = big.getElementsByTagName("img")[0]; //Right big image small = document.getElementById("small");// Container on the left //Get the width and height of the right visible area var bigW = big.clientWidth;
      var bigH = big.clientHeight;

      //Width and height of the large image on the right var bigImgW = bigImg.offsetWidth;
      var bigImgH = bigImg.offsetHeight;

      //Width and height of the small image on the left var smallImgW = smallimg.offsetWidth;
      var smallImgH = smallimg.offsetHeight;

      //Width and height of the left mask layer float.style.width = bigW/bigImgW * smallImgW + "px"; //175
      float.style.height = bigH/bigImgH * smallImgH/3*2 + "px";     

      //The maximum distance of mask layer movement float_maxJL_l = small.clientWidth -float.offsetWidth-10;
      float_maxJL_t = small.clientHeight - float.offsetHeight - 5;

      //The maximum distance the right picture moves bigImg_maxJL_l = bigImg.clientWidth - big.offsetWidth;
      bigImg_maxJL_t = bigImg.clientHeight - big.offsetHeight;

      big.style.display = "none";
      float.style.visibility = "hidden"; //The mouse has not moved into the left area, so the mask layer and the large image on the right are invisible},

    methods: {
    //Move the mouse into the left area to make the mask layer and the large image on the right visible over: function () {
        float.style.visibility = "visible";
        big.style.visibility = "visible";
        big.style.display = "block";
      }, 

      //Move the mouse out of the left area to make the mask layer and the large image on the right invisible out: function () {
        float.style.visibility = "hidden";
        big.style.display = "none";
      }, 

      //When the mouse moves, the mask layer moves with the mouse move: function (ev) {
        var l = ev.clientX - small.offsetLeft - float.offsetWidth/2;
        var t = ev.clientY - small.offsetTop - float.offsetHeight/2;

        if( l < 0 ) l = 0; // value is set to 0 if it exceeds the left boundary
        if( t < 0 ) t = 0; // value exceeds the upper boundary and is set to 0

        if( l > float_maxJL_l ) l = float_maxJL_l; //Limit its range of movement within the container if( t > float_maxJL_t ) t = float_maxJL_t;

        //Find a ratio var scaleL = l/float_maxJL_l;
        var scaleT = t/float_maxJL_t;

         //Mask layer movement position float.style.left = l + "px";
        float.style.top = t + "px"; 

         //Movement position of the big picture on the right side bigImg.style.left = -scaleL * bigImg_maxJL_l + "px";
        bigImg.style.top = -scaleT * bigImg_maxJL_t + "px";
      },
    },
  }
</script>

//css style <style lang="scss" rel="stylesheet/scss" scoped>
  @import 'src/assets/scss/variable.scss';
  #float {
    width: 120px;
    height: 120px;
    position: absolute; //Requiredbackground: $primary;
    border: 1px solid #ccc;
    opacity: 0.5;
    cursor:move;
  }
  #big {
    position: absolute; //Required top: 260px;
    left: 37.6%;
    width: 350px;
    height: 500px;
    overflow: hidden;
    border: 1px solid #ccc;
    background: #ffffff;
    z-index: 1;
    visibility: hidden;
  }
  #small {
    position: relative; //Required z-index: 1;
    img{
      width:340px;
      height:320px;
    }
  }
  #big img {
    position: absolute; //Required z-index: 5;
    width:700px;
    height:700px;
  }
</style>

In CSS, you need to pay attention to setting the position of each image and mask layer.

Mask layer analysis:

The width (height) of the left mask layer = the width (height) of the right visible area / the width (height) of the right large image * the width (height) of the left small image
(The mask layer can be understood as simulating the large image box on the right. A large image is placed in the large image box on the right, and then the floating area is obtained according to a certain ratio. At the same time, the box is set to overflow and hide. The presentation ratio of the large image on the right relative to the right container corresponds to the ratio of the mask layer on the left relative to the left container.)
The distance the mask layer moves = the width (height) of the left container - the width (height) of the mask layer; (so that it always moves in the left container)
When the mouse moves to the small picture box on the left, we need to ensure that the mouse is always in the mask layer, and the mask layer moves with the movement of the mouse (implying that the position of the mask layer is closely related to the coordinates of the mouse when it moves, and it cannot overflow the container on the left. See the code for the calculation method).

Note: There is a hidden bug here, that is, when your interface scrolls, the mask layer will not move with the scrolling of the interface. When the interface scrolls down, the mouse is in the left container but no longer in the mask layer area, and the mask layer can no longer be moved. The solution is as follows:

move = function (ev){
        var scroll = this.getClientHeight(); //Get the current interface moving position var l = ev.clientX - small.offsetLeft - float.offsetWidth/2;
        var t = ev.clientY - small.offsetTop - float.offsetHeight/2;

        t=t+scroll; //The height of the mask layer should be added to the scrolling height of the interface accordingly if( l < 0 ) l = 0;
        if( t < 0 ) t = 0; 

        if( l > float_maxJL_l ) l = float_maxJL_l;
        if( t > float_maxJL_t ) t = float_maxJL_t;

        var scaleL = l/float_maxJL_l;
        var scaleT = t/float_maxJL_t;

        float.style.left = l + "px";
        float.style.top = t + "px";

        bigImg.style.left = -scaleL * bigImg_maxJL_l + "px";
        bigImg.style.top = -scaleT * bigImg_maxJL_t + "px";
},
//Method to get the scroll height of the interface getClientHeight: function(){
    var scrollTop=0;
    if(document.documentElement&&document.documentElement.scrollTop)
    {
      scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body)
    {
      scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}

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:
  • Example code of Vue3 encapsulated magnifying glass component
  • How to use Vue3 to achieve a magnifying glass effect example
  • Vue3 realizes the image magnifying glass effect
  • Vue implements the magnifying glass function of the product details page
  • Vue implements the magnifying glass effect of tab switching
  • Vue implements a simple magnifying glass effect
  • Vue implements a search box with a magnifying glass
  • Vue3.0 implements the magnifying glass effect case study
  • Vue3.0 handwriting magnifying glass effect
  • Vue implements magnifying glass effect
  • Detailed explanation of the product main picture magnifying glass solution based on Vue
  • A handwritten vue magnifying glass effect
  • Use of e-commerce image magnifier plug-in based on vue2.x
  • Vue realizes the product magnifying glass effect

<<:  Detailed explanation of MySQL multi-table query examples [link query, subquery, etc.]

>>:  Linux uses Rsync+Inotify to achieve real-time synchronization of local and remote data

Recommend

Cross-browser development experience summary (I) HTML tags

Add a DOCTYPE to the page Since different browser...

Summary of MySQL Undo Log and Redo Log

Table of contents Undo Log Undo Log Generation an...

Summary of basic SQL statements in MySQL database

This article uses examples to describe the basic ...

How to run multiple MySQL instances in Windows

Preface In Windows, you can start multiple MySQL ...

foreman ubuntu16 quick installation

Quickstart Guide The Foreman installer is a colle...

Detailed explanation of MySQL custom functions and stored procedures

Preface This article mainly introduces the releva...

Introduction and use of js observer mode

Table of contents I. Definition 2. Usage scenario...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

Examples of using html unordered list tags and ordered list tags

1. Upper and lower list tags: <dl>..</dl...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...

In-depth understanding of Linux load balancing LVS

Table of contents 1. LVS load balancing 2. Basic ...