PrefaceWhen 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
2. How to encapsulate? 1. PreparationYou 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 packagingJust 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. UseHere 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 DemonstrationMove 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~) SummarizeFor 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:
|
<<: How to export mysql table structure to excel
>>: How to use filters to implement monitoring in Zabbix
view: When a temporary table is used repeatedly, ...
In daily work, we may encounter a layout like thi...
background When we talk about transactions, every...
【Foreword】 Recently I want to stress test ITOO...
Table of contents 1. Introduction to priority que...
After the National Day holiday, did any of you fi...
This article shares the specific code of videojs+...
This article shares the specific code of Vue to s...
There are two common ways to make div background ...
Basic knowledge of responsive layout development ...
gzip is a command often used in Linux systems to ...
1. Set the list symbol list-style-type: attribute...
Today I deployed the free-installation version of...
There are two ways to install nodejs in linux. On...
I had been using MySQL 5.7 before, but because My...