vue项目商品图片放大镜功能

 

<template>

  <div class="spec-preview">

    <!-- 图片地址 -->

    <img src="./assets/7f34f54ecc034f118655ded9cff02770.png" />

    <!-- 定义鼠标事件 -->

    <div class="event" @mousemove="handler"></div>

    <!-- //定义放大后的图样式 -->

    <div class="big">

      <!-- 放大后的图的原始图片 -->

      <img src="./assets/7f34f54ecc034f118655ded9cff02770.png" ref="big" />

    </div>

    <!-- 遮罩层 -->

    <div class="mask" ref="mask"></div>

  </div>

</template>

<script>

export default {

  name: "Zoom",

  data() {

    return {

      currentIndex: 0,

    };

  },

  methods: {

    //这里是放大方法的主要处理逻辑

    handler(event) {

      let mask = this.$refs.mask;

      let big = this.$refs.big;

      let left = event.offsetX - mask.offsetWidth / 2;

      let top = event.offsetY - mask.offsetHeight / 2;

      //约束范围

      if (left <= 0) left = 0;

      if (left >= mask.offsetWidth) left = mask.offsetWidth;

      if (top <= 0) top = 0;

      if (top >= mask.offsetHeight) top = mask.offsetHeight;

      mask.style.left = left + "px";

      mask.style.top = top + "px";

      big.style.left = -2 * left + "px";

      big.style.top = -2 * top + "px";

      console.log(left);

    },

  },

};

</script>

<style lang="less">

.spec-preview {

  position: relative;

  width: 400px;

  height: 400px;

  border: 1px solid #ccc;

  img {

    width: 100%;

    height: 100%;

  }

  .event {

    width: 100%;

    height: 100%;

    position: absolute;

    top: 0;

    left: 0;

    z-index: 998;

  }

  .mask {

    width: 50%;

    height: 50%;

    background-color: rgba(171, 250, 171, 0.3);

    position: absolute;

    left: 0;

    top: 0;

    display: none;

  }

  .big {

    width: 100%;

    height: 100%;

    position: absolute;

    top: -1px;

    left: 100%;

    border: 1px solid #aaa;

    overflow: hidden;

    z-index: 998;

    display: none;

    background: white;

    img {

      width: 200%;

      max-width: 200%;

      height: 200%;

      position: absolute;

      left: 0;

      top: 0;

    }

  }

  .event:hover ~ .mask,

  .event:hover ~ .big {

    display: block;

  }

}

</style>

实现淘宝图片放大镜功能,可以通过以下步骤实现: 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` 对话框。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值