Vue3 encapsulates the magnifying glass effect component of Jingdong product details page

Vue3 encapsulates the magnifying glass effect component of Jingdong product details page

This article shares the specific code of vue3 encapsulation similar to the magnifying glass effect component of Jingdong product details page for your reference. The specific content is as follows

First, complete the basic layout


Complete the image switching effect and switch images through the mouseenter event


Landing code

<template>
  <div class="goods-image">
    <!-- Preview large image -->
    <div class="large" v-show="show" :style="[{ backgroundImage: `url(${images[currIndex]})` }, bgPosition]"></div>
    <!-- Product image-->
    <div class="middle">
      <!-- Product image-->
      <img ref="target" :src="images[currIndex]" alt="" />
      <!-- The mouse enters the mask of the image-->
      <div class="layer" v-show="show" :style="[position]"></div>
    </div>
    <!-- Thumbnail -->
    <ul class="small">
      <li v-for="(img, i) in images" :key="img" :class="{ active: i === currIndex }">
        <!-- Move the mouse to the small picture next to the large picture of the product and the large picture will be displayed-->
        <img @mouseenter="currIndex = i" :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images:
      type: Array,
      default: () => []
    }
  },
  setup(props) {
    // Control the display and hiding of the preview image and mask layer const show = ref(false)
    // Control which image is displayed in the product image const currIndex = ref(0)
    // ref gets the DOM element const target = ref(null)
    // Record the coordinate position of the masked semi-transparent image in the product image const position = reactive({
      top: 0,
      left: 0
    })
    // Record the coordinate position of the area covered by the mask layer in the preview image const bgPosition = reactive({
      backgroundPositionX: 0,
      backgroundPositionY: 0
    })
    // The method useMouseInElement provided by the third-party vueuse gets the coordinates of the mouse in a certain area const { elementX, elementY, isOutside } = useMouseInElement(target)
    // The listener monitors the coordinates of the mouse when it enters the product image to manipulate the mask layer and the preview image effect watch([elementX, elementY, isOutside], () => {
      // When isisOutside.value is true, it means the mouse has not entered the target element. When it is false, it means the mouse has entered the target element. // When it is true, the coordinates are not recorded to avoid performance loss if (isOutside.value) {
        // The mask layer and preview image will not be displayed if the mouse does not enter the target element show.value = false
        return
      }
      // When the mouse enters the target element, the mask layer and the preview image are displayed.show.value = true
      // Determine the boundary value based on the size of the mask layer and the size of the product image // The boundary value of left (left, right)
      if (elementX.value < 100) {
        position.left = 0
      } else if (elementX.value > 300) {
        position.left = 200
      } else {
        position.left = elementX.value - 100
      }
      // Boundary values ​​of top (upper and lower)
      if (elementY.value < 100) {
        position.top = 0
      } else if (elementY.value > 300) {
        position.top = 200
      } else {
        position.top = elementY.value - 100
      }
      // The coordinates of the part of the product image covered by the mask layer in the preview image, plus the unit bgPosition.backgroundPositionY = -position.top * 2 + 'px'
      bgPosition.backgroundPositionX = -position.left * 2 + 'px'
      // The mask layer is relative to the coordinates of the upper left corner of the product image, plus the unit position.top += 'px'
      position.left += 'px'
    })
    // Return to the template using return { currIndex, show, target, position, bgPosition }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    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;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>

Final result

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:
  • Vue realizes the product magnifying glass effect
  • 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
  • Vue implements a search box with a magnifying glass
  • Vue3.0 handwriting magnifying glass effect
  • Vue implements magnifying glass effect
  • A handwritten vue magnifying glass effect

<<:  Example of implementing load balancing with Nginx+SpringBoot

>>:  Mysql delete data and data table method example

Recommend

WeChat Mini Program QR Code Generation Tool weapp-qrcode Detailed Explanation

WeChat Mini Program - QR Code Generator Download:...

Detailed explanation of the use of props in React's three major attributes

Table of contents Class Component Functional Comp...

LinkedIn revamps to simplify website browsing

Business social networking site LinkedIn recently...

Examples of correct judgment methods for data types in JS

Table of contents Preface Can typeof correctly de...

How to change the domestic source of Ubuntu 20.04 apt

UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Three ways to prevent MySQL from inserting duplicate data

Create a new table CREATE TABLE `person` ( `id` i...

Detailed explanation of MySQL group sorting to find the top N

MySQL group sorting to find the top N Table Struc...

How to modify Ubuntu's source list (source list) detailed explanation

Introduction The default source of Ubuntu is not ...

Implementing countdown effect with javascript

Use Javascript to achieve the countdown effect, f...

About the overlap of margin value and vertical margin in CSS

Margin of parallel boxes (overlap of double margi...