vue 组件封装 | 商品图片放大镜

目录

效果展示

1.封装组件 s-imgZoom.vue

2.使用范例

template部分

js部分

css部分


效果展示

1.封装组件 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>

2.使用范例

template部分

 <!-- 组件默认是100%的高宽,所以建议将组件包含在一个有固定高宽的容器内 -->
        <s-imgZoom :width="320" :height="320" :minIMGsrc="active_pic" :scale="3" />
  <!-- 缩略图渲染 -->
        <el-row class="smallPic">
          <el-col
            v-for="(item ,i) in img_pic_list"
            :key="i"
            :class="{'active_pic':(active_pic===item.link)}"
          >
            <el-image lazy @click="changeItem(item)" :src="item.link"></el-image>
          </el-col>
        </el-row>

js部分

  data () {
    return {
      // 图片数据数组
      img_pic_list: [
        {
          link: 'https://img.alicdn.com/imgextra/i2/2183295419/O1CN01awKYlS1ptwv0tbmL5_!!0-item_pic.jpg_430x430q90.jpg',
        },
        {
          link: 'https://img2.woyaogexing.com/2023/06/06/9060c27c86d514b6c13868a30e2e14fc.jpg',
        }
      ],
      active_pic: '', // 选中状态
    }
  },
 created () {
    // 初始化选中第一张
    this.active_pic = this.img_pic_list[0].link
  
  },
 methods: {
// 点击切换事件
    changeItem (item) {
      this.active_pic = item.link
    },
}

css部分

  //  缩略图
  .smallPic{
    width: 320px;
    height: 64px;
    overflow: hidden;
    /deep/.el-col{
      height: 64px;
      width: 20%;
      box-sizing: border-box;
      cursor: pointer;
      .el-image__inner{
        border-radius: 0;
      }
    }
    .active_pic{
      border: 1px solid orange;
    }
  }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue组件封装和复用是指将代码逻辑和功能封装在一个独立的组件中,并在需要的地方重复使用该组件的过程。 Vue组件封装可以通过以下步骤实现: 1. 创建组件:使用Vue框架提供的组件选项来创建一个组件。可以使用Vue.extend方法或者直接在Vue实例中定义一个组件。 2. 封装功能:在组件中添加业务逻辑、数据和方法等功能。可以通过计算属性、监听器、方法等实现具体的功能。 3. 编写模板:使用Vue的模板语法编写组件的结构和样式。通过将标签、属性和事件绑定到组件的数据和方法来实现交互效果。 4. 注册组件:将组件注册到Vue实例中,使其可以在其他组件中使用。可以使用Vue.component方法全局注册组件,也可以在局部组件中通过components选项注册组件。 5. 使用组件:在需要的地方使用组件,可以通过标签的方式将组件插入到页面中。 通过封装组件,可以将代码逻辑和UI元素进行有效地拆分和复用。例如,可以将页面中重复出现的按钮、表单、卡片等元素封装组件,通过复用组件来提高代码的可维护性和复用性。同时,组件化的思想也使得团队协作更加高效,不同开发者可以独立开发、测试和维护自己负责的组件,最终组合成完整的应用程序。 总之,Vue组件封装和复用是一种有效的开发方式,可以提高代码的可维护性和可复用性,同时也促进了团队协作和开发效率的提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值