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

简介: 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" />
目录
相关文章
|
16天前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
136 2
|
4月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
573 0
|
3月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
111 0
|
3月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
246 1
|
存储 前端开发 JavaScript
为什么我不再用Vue,改用React?
当我走进现代前端开发行业的时候,我做了一个每位开发人员都要做的决策:选择一个合适的框架。当时正逢 jQuery 被淘汰,前端开发者们不再用它编写难看的、非结构化的老式 JavaScript 程序了。
|
6月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
770 4
|
4月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
5月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
548 77
|
6月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
4月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
377 17