How to achieve Taobao magnifying glass effect in vue3
Achieve effect
Implementation idea
We need 4 steps to realize the effect of Taobao magnifying glass in animated pictures.
1. Complete the layout of the small picture box, mask, and large picture box
2. Implement the mouse movement to the small picture box to display the mask and the large picture box
3. Implement the mouse movement mask in Moving the small picture box
4. The movement range of the mask cannot exceed the small picture box, and the movement of the large picture box will drive the movement of the large picture box
Complete the layout of the small picture box, mask, and large picture box
My layout small picture box wraps pictures, masks, and large picture boxes.
The small picture box is relatively positioned. Masks, large picture boxes, and large picture pictures are all absolutely positioned (large picture pictures cannot be moved without positioning). Mask needs to set transparency. As long as the effect can be achieved.
Realize moving the mouse to the small picture box to display the mask and the large picture box
The small picture box is bound to the mouse move-in event (mouseover) and the mouse move-out event (mouseout). Switch the state each time the event is triggered.
Realize the movement of the mouse movement mask in the small picture box
The small picture box is bound to the mousemove mouse movement event. The x coordinate of the mouse on the page minus the offerLeft of the small picture box is the mouse on the left side of the box. This is the coordinate where the mask needs to be moved. If you want the mouse to be in the middle of the mask, divide it by 2. The same goes for the y coordinate.
The moving range of the mask cannot exceed the small picture box, and the movement of the large picture will drive the movement of the large picture.
Judgment of the boundary value, if it is less than or equal to 0, set the left of the mask to 0, and the x has a boundary value. It is the width of the small box minus the width of the mask. In the same way, the y-axis is the height of the small box minus the height of the mask.
There is a proportional relationship in driving the movement of the large image. The moving distance of the large picture = the moving distance of the mask * the maximum moving distance of the big box / the x maximum moving distance of the mask,
Complete code
<template> <div class="tb-booth" @mouseover="onMouseOver" @mouseout="onMouseOut" @mousemove="onMouseMove" ref="boothRef" > <img src="/static/imghw/default1.png" data-src="https://img.alicdn.com/imgextra/i3/1917047079/O1CN01lkG2pf22AEUi1owve_!!1917047079.png_430x430q90.jpg" class="lazy" /> <div class="mask" ref="mask" v-show="boxShow" /> <div class="big-img_box" ref="bigImgBox" v-show="boxShow"> <img class="big-img lazy" src="/static/imghw/default1.png" data-src="https://img.alicdn.com/imgextra/i3/1917047079/O1CN01lkG2pf22AEUi1owve_!!1917047079.png" ref="bigImg" /> </div> </div> </template> <script> import { reactive, toRefs, ref } from "vue"; export default { setup() { const boothRef = ref(null); const mask = ref(null); const bigImg = ref(null); const bigImgBox = ref(null); const state = reactive({ boxShow: false }); const onMouseOver = () => { state.boxShow = true; }; const onMouseOut = () => { state.boxShow = false; }; const onMouseMove = (e) => { let x = e.pageX - boothRef.value.offsetLeft; let y = e.pageY - boothRef.value.offsetTop; let maskX = x - mask.value.offsetWidth / 2; let maskY = y - mask.value.offsetHeight / 2; // mask的x最大移动距离 let maskXMaxMove = boothRef.value.offsetWidth - mask.value.offsetWidth; let maskYMaxMove = boothRef.value.offsetHeight - mask.value.offsetHeight; let bigImgXMaxMove = bigImgBox.value.offsetWidth - bigImg.value.offsetWidth; let bigImgYMaxMove = bigImgBox.value.offsetHeight - bigImg.value.offsetHeight; if (maskX <= 0) { maskX = 0; } else if (maskX >= maskXMaxMove) { maskX = maskXMaxMove; } if (maskY <= 0) { maskY = 0; } else if (maskY >= maskYMaxMove) { maskY = maskYMaxMove; } mask.value.style.left = maskX + "px"; mask.value.style.top = maskY + "px"; // 大图片移动距离 = mask的移动距离*大盒子最大移动距离 / mask的x最大移动距离 let bixImgXMove = (maskX * bigImgXMaxMove) / maskXMaxMove; let bixImgYMove = (maskY * bigImgYMaxMove) / maskYMaxMove; bigImg.value.style.left = bixImgXMove + "px"; bigImg.value.style.top = bixImgYMove + "px"; }; return { ...toRefs(state), boothRef, mask, bigImg, bigImgBox, onMouseOver, onMouseOut, onMouseMove, }; }, }; </script> <style scoped> .tb-booth { width: 430px; height: 430px; position: relative; border: 1px solid #cccccc; } .mask { position: absolute; top: 0; left: 0; width: 200px; height: 200px; background-color: rgb(61, 110, 206); opacity: 0.5; cursor: move; } .big-img_box { position: absolute; top: 0; left: 530px; width: 500px; height: 500px; background-color: #fff; border: 1px solid #cccccc; overflow: hidden; } .big-img { position: absolute; left: 0; top: 0; } </style>
The above is the detailed content of How to achieve Taobao magnifying glass effect in vue3. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











To achieve partial refresh of the page, we only need to implement the re-rendering of the local component (dom). In Vue, the easiest way to achieve this effect is to use the v-if directive. In Vue2, in addition to using the v-if instruction to re-render the local dom, we can also create a new blank component. When we need to refresh the local page, jump to this blank component page, and then jump back in the beforeRouteEnter guard in the blank component. original page. As shown in the figure below, how to click the refresh button in Vue3.X to reload the DOM within the red box and display the corresponding loading status. Since the guard in the component in the scriptsetup syntax in Vue3.X only has o

tinymce is a fully functional rich text editor plug-in, but introducing tinymce into vue is not as smooth as other Vue rich text plug-ins. tinymce itself is not suitable for Vue, and @tinymce/tinymce-vue needs to be introduced, and It is a foreign rich text plug-in and has not passed the Chinese version. You need to download the translation package from its official website (you may need to bypass the firewall). 1. Install related dependencies npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2. Download the Chinese package 3. Introduce the skin and Chinese package. Create a new tinymce folder in the project public folder and download the

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

Vue implements the blog front-end and needs to implement markdown parsing. If there is code, it needs to implement code highlighting. There are many markdown parsing libraries for Vue, such as markdown-it, vue-markdown-loader, marked, vue-markdown, etc. These libraries are all very similar. Marked is used here, and highlight.js is used as the code highlighting library. The specific implementation steps are as follows: 1. Install dependent libraries. Open the command window under the vue project and enter the following command npminstallmarked-save//marked to convert markdown into htmlnpmins

The final effect is to install the VueCropper component yarnaddvue-cropper@next. The above installation value is for Vue3. If it is Vue2 or you want to use other methods to reference, please visit its official npm address: official tutorial. It is also very simple to reference and use it in a component. You only need to introduce the corresponding component and its style file. I do not reference it globally here, but only introduce import{userInfoByRequest}from'../js/api' in my component file. import{VueCropper}from'vue-cropper&

Preface Whether it is vue or react, when we encounter multiple repeated codes, we will think about how to reuse these codes instead of filling a file with a bunch of redundant codes. In fact, both vue and react can achieve reuse by extracting components, but if you encounter some small code fragments and you don’t want to extract another file, in comparison, react can be used in the same Declare the corresponding widget in the file, or implement it through renderfunction, such as: constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(

vue3+ts+axios+pinia realizes senseless refresh 1. First download aiXos and pinianpmipinia in the project--savenpminstallaxios--save2. Encapsulate axios request-----Download js-cookienpmiJS-cookie-s//Introduce aixosimporttype{AxiosRequestConfig ,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess

After the vue3 project is packaged and published to the server, the access page displays blank 1. The publicPath in the vue.config.js file is processed as follows: const{defineConfig}=require('@vue/cli-service') module.exports=defineConfig({publicPath :process.env.NODE_ENV==='production'?'./':'/&
