vue 元件封裝 | 圖片放大鏡(同天貓、淘寶、京東等商品圖片放大瀏覽效果)
阿新 • • 發佈:2020-12-23
技術標籤:Vue獲取元素與螢幕的距離獲取滑鼠與螢幕的距離css放大圖片
目錄
封裝元件 s-imgZoom.vue
<template> <div style="display: flex;"> <div class="box" :style="minImgBoxStyle" @mouseleave="mouseLeave" @mouseenter="mouseEnter" @mousemove="mousemove($event)"> <!--原始照片-小照片--> <img :style="minImgStyle" fit="contain" ref="minImg" :src="finalMinIMGsrc"/> <!--探測塊--> <div v-show="show" class="areaMark" :style="areaMarkStyle"></div> </div> <div class="box" :style="maxImgBoxStyle" v-show="show"> <!--放大後的照片--> <img :style="maxImgStyle" fit="contain" :src="finalMaxIMGsrc"/> </div> </div> </template> <script> export default { props: { minIMGsrc: String, maxIMGsrc: String, scale: { type: Number, default: 2 }, width: { type: Number, default: 420 }, height: { type: Number, default: 420 }, }, data() { return { show: false, finalMinIMGsrc: '', finalMaxIMGsrc: '', imgBoxWidth: 420, imgBoxHeight: 420, areaWidth: 210, areaHeight: 210, areaMarkStyle: {}, minImgBoxStyle: { cursor: 'move' }, minImgStyle: {}, maxImgBoxStyle: {}, maxImgStyle: { position: 'absolute', }, } }, mounted() { this.imgBoxWidth = this.width this.imgBoxHeight = this.height this.$set(this.minImgStyle, 'width', this.imgBoxWidth + 'px') this.$set(this.minImgStyle, 'height', this.imgBoxHeight + 'px') this.$set(this.maxImgStyle, 'width', this.imgBoxWidth + 'px') this.$set(this.maxImgStyle, 'height', this.imgBoxHeight + 'px') this.$set(this.minImgBoxStyle, 'width', this.imgBoxWidth + 'px') this.$set(this.minImgBoxStyle, 'height', this.imgBoxHeight + 'px') this.$set(this.maxImgBoxStyle, 'width', this.imgBoxWidth + 'px') this.$set(this.maxImgBoxStyle, 'height', this.imgBoxHeight + 'px') this.areaWidth = this.imgBoxWidth / this.scale this.areaHeight = this.imgBoxHeight / this.scale this.finalMinIMGsrc = this.minIMGsrc if (!this.maxIMGsrc) { this.finalMaxIMGsrc = this.minIMGsrc } this.$set(this.areaMarkStyle, 'width', this.areaWidth + 'px') this.$set(this.areaMarkStyle, 'height', this.areaHeight + 'px') this.$set(this.maxImgStyle, 'transform', 'scale(' + this.scale + ')') }, methods: { mouseEnter() { this.show = true }, mouseLeave() { this.show = false }, mousemove(e) { // 獲取文件頂端與螢幕頂部之間的距離 // scrollTop指的是“元素中的內容”超出“元素上邊界”的那部分的高度 let documentScrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 獲取滑鼠相對於螢幕的座標 let mouseClientX = e.clientX let mouseClientY = e.clientY // 獲取小照片相對於螢幕位置資訊 // getBoundingClientRect()用於獲得頁面中某個元素的左,上,右和下分別相對瀏覽器視窗的位置。 let minImgPosition = this.$refs.minImg.getBoundingClientRect(); let minImgX = minImgPosition.left; let minImgY = minImgPosition.top; // 計算出探測塊相對於小圖片的座標 let areaLeft = mouseClientX - minImgX - this.areaWidth / 2 let areaTop = mouseClientY - minImgY - this.areaHeight / 2 if (documentScrollTop > 0) { areaTop = documentScrollTop + areaTop } let minLeft = 0 let maxLeft = this.imgBoxWidth - this.areaWidth let minTop = 0 let maxTop = this.imgBoxHeight - this.areaHeight // 禁止探測塊移出小圖片 if (areaLeft < minLeft) { areaLeft = minLeft } if (areaLeft > maxLeft) { areaLeft = maxLeft } if (areaTop < minTop) { areaTop = minTop } if (areaTop > maxTop) { areaTop = maxTop } // 更新探測塊的座標 this.$set(this.areaMarkStyle, 'left', areaLeft + 'px') this.$set(this.areaMarkStyle, 'top', areaTop + 'px') // 更新放大後照片的座標 this.$set(this.maxImgStyle, 'left', (this.scale - 1) * this.imgBoxWidth / 2 - areaLeft * this.scale + 'px') this.$set(this.maxImgStyle, 'top', (this.scale - 1) * this.imgBoxHeight / 2 - areaTop * this.scale + 'px') } } } </script> <style scoped> .box { border: 1px solid darkgray; position: relative; overflow: hidden; box-sizing: border-box; } .areaMark { position: absolute; background: url(//img-tmdetail.alicdn.com/tps/i4/T12pdtXaldXXXXXXXX-2-2.png); } </style>
使用範例
<s-imgZoom :width="300" :height="300" minIMGsrc="https://img.alicdn.com/imgextra/i2/2183295419/O1CN01awKYlS1ptwv0tbmL5_!!0-item_pic.jpg_430x430q90.jpg" :scale="3" />