vue实现点击图片,图片放大效果

vue实现点击图片,图片放大效果

  1. 首先在template中插入img,并设置点击事件,这个时候是小图标
<template>
  <div>
    <img src="@/assets/images/avatar.png" @click="imgShow()" />  </div>
</template>
  1. data定义点击放大的图片url,imageUrl为点击后放大的大图的url
data() {
    return {
      imageUrl: require('@/assets/images/avatar.png')
    }
  },
  1. 定义方法
methods: {
    imgShow() {
      const image = new Image()
      image.src = this.imageUrl
      image.onload = () => {
        //创建弹出层
        const previewContatiner = document.createElement('div')
        previewContatiner.style.position = 'fixed'
        previewContatiner.style.top = 0
        previewContatiner.style.bottom = 0
        previewContatiner.style.left = 0
        previewContatiner.style.right = 0
        previewContatiner.style.zIndex = 9999
        previewContatiner.style.backgroundColor = 'rgba(0,0,0,0.8)'
        previewContatiner.style.display = 'flex'
        previewContatiner.style.justifyContent = 'center'
        previewContatiner.style.alignItems = 'center'
        document.body.appendChild(previewContatiner)
        //在弹出层增加图片
        const previewImage = document.createElement('img')
        previewImage.src = this.imageUrl
        previewImage.style.maxWidth = '80%'
        previewImage.style.maxHeight = '80%'
        previewImage.style.zIndex = 9999
        previewContatiner.appendChild(previewImage)
        //点击弹出层,关闭预览
        previewContatiner.addEventListener('click', () => {
          document.body.removeChild(previewContatiner)
        })
      }
      image.onerror = function () {
        console.log('图片加载失败')
      }
    }
  }

原文链接:https://www.cnblogs.com/zengyu123/p/17807768.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值