How to use Vue3 to achieve a magnifying glass effect example

How to use Vue3 to achieve a magnifying glass effect example

Preface

When browsing shopping websites, I believe everyone has seen that when you put the mouse on a product, there will be a magnifying effect. Today we will encapsulate a global component with a magnifying glass effect by ourselves. Let’s take a look~

1. The significance of encapsulation

  • From a technical perspective
    • It is encapsulated as a global component through the vue plug-in method, and can be used in other locations of the entire project, and it is easy to use
    • Modular development concept, one module realizes one function
  • User perspective
    • Can bring a better browsing experience
    • You can see the details of the product

2. How to encapsulate?

1. Preparation

You need to use the useMouseInElement method of @vueuse/core, so first open the terminal in the project root directory and execute the following command

The specified version installed here, please choose according to your needs

npm install @vueuse/[email protected]

2. Start packaging

Just like the previous article, use the vue plugin to register global components

Store the encapsulated global components in src/components and create a new enlarge-images.vue file in this directory.

The code is as follows (example):

<template>
  <div class="goods-image">
    <!-- Preview large image -->
    <div class="large" :style='[{backgroundImage: `url(${images[currIndex]})`}, bgPosition]' v-show='isShow'></div>
    <div class="middle" ref='target'>
      <!-- Large image on the left -->
      <img :src="images[currIndex]" alt="">
      <!-- Mask layer-->
      <div class="layer" :style='[position]' v-show='isShow'></div>
    </div>
    <ul class="small">
      <!-- Thumbnail on the right-->
       <li v-for="(img,i) in images" :key="img" :class="{active:i===currIndex}">
        <img @mouseenter="currIndex=i" :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch, reactive } from 'vue'
import { useMouseInElement } from '@vueuse/core'

export default {
  name: 'EnlargeImages',
  props: {
    images:
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    const currIndex = ref(0)
    const target = ref(null)
    const isShow = ref(false)
    //Coordinates of the mask layer const position = reactive({
      left: 0,
      top: 0
    })
    // Control the position of the background image const bgPosition = reactive({
      backgroundPositionX: 0,
      backgroundPositionY: 0
    })
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // Listen for mouse movement information watch([elementX, elementY, isOutside], () => {
      // Every time the value changes, read the new data isShow.value = !isOutside.value
      // The mouse is outside the image area, no need to calculate coordinates if (isOutside.value) return
      // Horizontal directionif (elementX.value < 100) {
        // Left border position.left = 0
      } else if (elementX.value > 300) {
        // Right border position.left = 200
      } else {
        // The middle state position.left = elementX.value - 100
      }
      // Vertical directionif (elementY.value < 100) {
        // Upper border position.top = 0
      } else if (elementY.value > 300) {
        // Bottom border position.top = 200
      } else {
        // The middle state position.top = elementY.value - 100
      }
      // console.log(elementX.value, elementY.value, isOutside.value)
      // Calculate the position of the preview background bgPosition.backgroundPositionX = -position.left * 2 + 'px'
      bgPosition.backgroundPositionY = -position.top * 2 + 'px'
      // Calculate the left mask layer position position.left += 'px'
      position.top += 'px'
    })
    return { currIndex, target, isShow, position, bgPosition }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  box-sizing: border-box;
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
   img {
        width: 100%;
        height: 100%;
    }
  .large {
    position: absolute;
    top: 0;
    left: 410px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    margin: 0;
    padding: 0;
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin: 10px;
      list-style: none;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid #27ba9b;
      }
    }
  }
}
</style>

Create a new index.js under src/components

import EnlargeImages from './enlarge-images.vue'

export default {
  install (app) {
    app.component(EnlargeImages.name, EnlargeImages)
  }
}

Register as a plugin in main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// Own encapsulation import myUI from './components'

createApp(App).use(store).use(router).use(myUI).mount('#app')

3. Use

Here we use fixed data for testing

The code is as follows (example):

<template>
  <div class="home-banner">
    <!-- Magnifying glass effect-->
    <enlarge-images :images="images"/>
  </div>
</template>

<script>

export default {
  name: 'App',
  setup() {
    const images = [
      'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fcloud.jpeg',
      'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fground.jpeg',
      'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fnight.jpeg',
      'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fstreet.jpeg',
      'https://code-1307161657.cos.ap-beijing.myqcloud.com/images%2Fsun.jpeg'
    ]

    return { images }
  }
}
</script>

<style lang="less">
.home-banner {
  width: 1000px;
  margin: 50px auto;
}
</style>

3. Effect Demonstration

Move the mouse to the small picture on the right to switch the currently displayed picture

Put the mouse into the image preview area on the left, and move the mouse in the preview area to see the enlarged designated area on the right.

(PS: The gif image is too large, please take a look at the effect picture~)

Summarize

For the method of batch registration as global components, you can refer to the article Vue Common Tool Functions.

This is the end of this article on how to use vue3 to achieve a magnifying glass effect. For more relevant content about vue3 to achieve a magnifying glass effect, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example code of Vue3 encapsulated magnifying glass component
  • 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
  • Detailed explanation of the encapsulation and use of the Vue image magnifier component
  • 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

<<:  How to export mysql table structure to excel

>>:  How to use filters to implement monitoring in Zabbix

Recommend

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

Complete steps to install boost library under linux

Preface The Boost library is a portable, source-c...

Seven Principles of a Skilled Designer (1): Font Design

Well, you may be a design guru, or maybe that'...

How to view image information in Docker

In this article, we will need to learn how to vie...

Vue realizes the logistics timeline effect

This article example shares the specific code of ...

Talking about ContentType(s) from image/x-png

This also caused the inability to upload png files...

Detailed explanation of Docker data backup and recovery process

The data backup operation is very easy. Execute t...

JS realizes the effect of picture waterfall flow

This article shares the specific code of JS to re...

How to set focus on HTML elements

Copy code The code is as follows: <body <fo...

Analysis of the principle and usage of MySQL continuous aggregation

This article uses examples to illustrate the prin...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...