vue 组件封装 | 图片放大镜(同天猫、淘宝、京东等商品图片放大浏览效果)

目录

封装组件 s-imgZoom.vue

使用范例


封装组件 s-imgZoom.vue

<template>
    <div style="display: flex;position: relative">
        <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 maxImgBox" :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',
                },
            }
        },
        watch: {
            'minIMGsrc'() {
                this.init()
            },
            'maxIMGsrc'() {
                this.init()
            },
        },
        mounted() {
            this.init()
        },
        methods: {
            init() {
                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.$set(this.maxImgBoxStyle, 'left', this.imgBoxWidth + '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 + ')')
            },
            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);
    }
    .maxImgBox{
        position: absolute;
    z-index:999
    }
</style>

使用范例

<s-imgZoom  :width="300"  :height="300" minIMGsrc="https://img.alicdn.com/imgextra/i2/2183295419/O1CN01awKYlS1ptwv0tbmL5_!!0-item_pic.jpg_430x430q90.jpg" :scale="3" />

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现淘宝图片放大镜功能,可以通过以下步骤实现: 1. 在 Vue 组件中引入所需的依赖库,例如 `element-ui` 等。 2. 在模板中,将需要实现放大镜效果图片绑定到一个 `<div>` 元素上,并设置其样式。 3. 在该 `<div>` 元素上绑定 `@mousemove` 事件,监听鼠标在该元素上的移动。 4. 在事件处理函数中,获取鼠标元素上的坐标,并根据元素的大小和图片的大小计算出放大镜的位置。 5. 使用 `element-ui` 中的 `Dialog` 组件,将放大图片作为弹出框的内容,并设置其样式,并在需要放大图片上绑定 `@click` 事件,当点击该图片时,弹出放大镜。 下面是一个简单的 Vue 组件示例代码: ```html <template> <div class="image-container" @mousemove="handleMouseMove"> <img src="./image.jpg" alt="image" class="image" @click="showDialog"> <el-dialog :visible.sync="dialogVisible" width="60%"> <img src="./image.jpg" alt="image" class="dialog-image"> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, zoomedImageStyle: { position: 'absolute', border: '1px solid #ccc', width: '200px', height: '200px', display: 'none', zIndex: 9999 } } }, methods: { handleMouseMove(e) { const { clientX, clientY } = e const { left, top, width, height } = e.target.getBoundingClientRect() const x = clientX - left const y = clientY - top const zoomedImage = document.querySelector('.zoomed-image') const percentageX = x / width const percentageY = y / height const imageWidth = zoomedImage.width const imageHeight = zoomedImage.height const leftOffset = -percentageX * (imageWidth - width) const topOffset = -percentageY * (imageHeight - height) this.zoomedImageStyle.display = 'block' this.zoomedImageStyle.left = `${x}px` this.zoomedImageStyle.top = `${y}px` this.zoomedImageStyle.backgroundImage = `url(${zoomedImage.src})` this.zoomedImageStyle.backgroundSize = `${imageWidth}px ${imageHeight}px` this.zoomedImageStyle.backgroundPositionX = `${leftOffset}px` this.zoomedImageStyle.backgroundPositionY = `${topOffset}px` }, showDialog() { this.dialogVisible = true } } } </script> <style> .image-container { position: relative; } .image { width: 100%; height: auto; } .dialog-image { width: 100%; height: auto; } .zoomed-image { position: absolute; border: 1px solid #ccc; width: 200px; height: 200px; display: none; z-index: 9999; } </style> ``` 在该示例代码中,通过绑定 `@mousemove` 事件,监听鼠标元素上的移动,并在事件处理函数中计算出放大镜的位置,然后通过设置 `zoomedImageStyle` 对象的属性来实现放大镜。同时,在需要放大图片上绑定 `@click` 事件,当用户点击该图片时,弹出包含放大图片的 `Dialog` 对话框。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝阳39

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值