前端实现图片放大镜效果(vue)

文章描述了一个使用Vue.js编写的代码片段,展示了如何在网页上创建一个可响应缩放的图片裁剪工具,包括鼠标移动和离开事件处理,以及图片尺寸计算和放大镜位置设置。
摘要由CSDN通过智能技术生成

代码如下: 

<template>
  <div class="img_wrapper" ref="imgWrapper">
    <img
      class="original_img"
      src="@/assets/images/testImg.jpg"
      alt="测试图片"
      @mousemove="handleMouseMove"
      @mouseleave="handleMouseLeave"
      @contextmenu.prevent.stop
      @load="initImage"
      ref="refImg"
    />
    <div
      v-if="isInside"
      :style="{
        backgroundSize: `${imgRealWidth * realGlassScale}px ${
          imgRealHeight * realGlassScale
        }px`,
        ...glassStyle
      }"
      class="cx-cropper-selection-glass"
    >
      <div class="cx-cropper-selection-glass__line is-horizontal"></div>
      <div class="cx-cropper-selection-glass__line is-vertical"></div>
      <div class="cx-cropper-selection-glass__mark"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isInside: false,
      imgRealWidth: 0, // 图片实际宽度
      imgRealHeight: 0, // 图片实际高度
      realGlassScale: 2,
      glassStyle: {
        // 放大镜坐标
        backgroundPosition: '0 0',
        left: 0,
        top: 0
      }
    }
  },

  methods: {
    initImage() {
      this.$nextTick(() => {
        this.imgRealWidth = this.$refs.refImg.clientWidth
        this.imgRealHeight = this.$refs.refImg.clientHeight
      })
    },
    handleMouseMove(e) {
      this.isInside = true
      this.setGlassPosition(e.offsetX, e.offsetY)
    },
    handleMouseLeave() {
      this.isInside = false
    },
    setGlassPosition(left, top) {
      const { realGlassScale } = this
      this.glassStyle.backgroundPosition = `-${left * realGlassScale - 75}px -${
        top * realGlassScale - 50
      }px`
      this.glassStyle.left = left + 10 + 'px'
      this.glassStyle.top = top + 10 + 'px'
    }
  }
}
</script>

<style lang="scss" scoped>
.img_wrapper {
  margin: 0 auto;
  width: 600px;
  border: 1px solid red;
  position: relative;
  .original_img {
    width: 100%;
    cursor: crosshair;
  }

  .cx-cropper-selection-glass {
    width: 150px;
    height: 100px;
    position: absolute;
    border: 1px solid #000;
    z-index: 30;
    background: url('@/assets/images/testImg.jpg');
    pointer-events: none;
    background-repeat: no-repeat;
    box-sizing: border-box;
    background-color: #fff;
    .cx-cropper-selection-glass__line {
      position: absolute;
      background: rgba(#447ed9, 0.5);

      &.is-vertical {
        width: 6px;
        height: 100%;
        top: 0;
        left: 50%;
        transform: translateX(-50%);
      }

      &.is-horizontal {
        width: 100%;
        height: 6px;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
      }
    }

    .cx-cropper-selection-glass__mark {
      width: 5px;
      height: 5px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #000;
      background: #fff;
    }
  }
}
</style>

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现淘宝图片放大镜功能,可以通过以下步骤实现: 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` 对话框。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值