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

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

Sample code for implementing rolling updates of services using Docker Swarm

1. What is Docker Swarm? Docker Swarm is a cluste...

Win10 64-bit MySQL8.0 download and installation tutorial diagram

How do I download MySQL from the official website...

Page Refactoring Skills - Javascript, CSS

About JS, CSS CSS: Stylesheet at the top Avoid CS...

Example code of how CSS matches multiple classes

CSS matches multiple classes The following HTML t...

How to obtain root permissions in a docker container

First, your container must be running You can vie...

Vue implements tree table

This article example shares the specific code of ...

Vue-cli framework implements timer application

Technical Background This application uses the vu...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Implementation of Nginx forwarding matching rules

1. Regular expression matching ~ for case-sensiti...

CSS achieves footer "bottom absorption" effect

We often encounter this problem: how to use CSS t...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...