zoukankan      html  css  js  c++  java
  • vue 实现像web淘宝一样区域放大功能

    vue 实现像web淘宝一样区域放大功能

    效果是这个样子的

    在这里插入图片描述

    直接上自定义组件代码:

    <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
                },
                 {
                    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" />
    

    亲测可用

    在这里插入图片描述

    【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处!
    【重要说明】本文为本菜鸟的学习记录,论点和观点仅代表个人不代表此技术的真理,目的是学习和可能成为向别人分享的经验,因此有错误会虚心接受改正,但不代表此时博文无误!
    【博客园地址】JayveeWong: http://www.cnblogs.com/wjw1014
    【CSDN地址】JayveeWong: https://blog.csdn.net/weixin_42776111
    【Gitee地址】Jayvee:https://gitee.com/wjw1014
    【GitHub地址】Jayvee:https://github.com/wjw1014
  • 相关阅读:
    C# Firefox Session Manager 文件的导出与管理
    安徒生的童话《冰雪皇后》原本是这样的
    许多人不知道的生活小秘方
    洗衣服窍门大全
    小窍门解决大问题(绝对值得收藏)
    日常生活小技巧
    谷歌浏览器应用商店打不开,下载不了扩展程序的解决办法
    食品安全如何让百姓放心
    把 WORD 里的换行符(向下的箭头)换成回车符(常用回车符)
    充满创意的生活小妙招 --爱生活爱创意!
  • 原文地址:https://www.cnblogs.com/wjw1014/p/15048597.html
Copyright © 2011-2022 走看看