vue的图片预览 点击按钮或者别的地方实现大图预览el-image

问题:实现点击按钮或者别的地方 能预览大图

解决:我使用的是el-image的功能 能进行大图预览 代码如下

<div class="swiper-slide swiper-item" v-for="(i, index) in list" :key="index">
  // 我是用的图片 但是你可以写成按钮 点击事件
  <img class="img"  @click="handleImgClick(i)">
</div>
// 用的是el-image组件提供的功能
<el-image ref="elImageRef" v-show="false" class="img" :src="imgUrl" :preview-src- list="imgList">
</el-image>
// 这个是方法 我用的是vue2 用vue3的自己改一下就行 简单
    handleImgClick(i) {
      this.imgList = i.images.map(i => i.address) // 需要的预览列表图片地址
      this.$refs.elImageRef.showViewer = true // 这个属性就可以控制 预览视图打开
    }

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现点击图片放大预览的功能,可以通过绑定点击事件,在点击时弹出模态框或者新窗口来显示预览图片。在模态框或者新窗口中,我们可以展示图片的原始大小,并提供一些交互操作,如放大、缩小、旋转、裁剪等。 下面是一个使用VueElement UI实现图片放大预览的代码示例: ```html <template> <div> <input type="file" accept="image/*" capture="camera" @change="previewImage" ref="fileInput"> <img :src="imageUrl" v-if="imageUrl" @click="showPreview"> <el-dialog :visible.sync="previewVisible"> <img :src="imageUrl" :style="{transform: 'rotate(' + rotateDegree + 'deg) scale(' + scale + ')', cursor: isDragging ? 'grabbing' : 'grab'}" @mousedown="startDrag" @mouseup="endDrag" @mousemove="onDrag" ref="previewImage"> <el-slider v-model="scale" :min="0.1" :max="5" :step="0.1" style="width: 300px; margin: 10px 0;"></el-slider> <el-button-group> <el-button type="primary" @click="rotateDegree -= 90">向左旋转</el-button> <el-button type="primary" @click="rotateDegree += 90">向右旋转</el-button> </el-button-group> </el-dialog> </div> </template> <script> export default { data() { return { imageUrl: '', previewVisible: false, rotateDegree: 0, scale: 1, isDragging: false, startX: 0, startY: 0, startLeft: 0, startTop: 0, }; }, methods: { previewImage(event) { const file = event.target.files[0]; if (!file) { return; } const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = (e) => { this.imageUrl = e.target.result; }; }, showPreview() { this.previewVisible = true; }, startDrag(event) { this.isDragging = true; this.startX = event.clientX; this.startY = event.clientY; this.startLeft = this.$refs.previewImage.offsetLeft; this.startTop = this.$refs.previewImage.offsetTop; }, endDrag() { this.isDragging = false; }, onDrag(event) { if (!this.isDragging) { return; } const dx = event.clientX - this.startX; const dy = event.clientY - this.startY; const left = this.startLeft + dx; const top = this.startTop + dy; this.$refs.previewImage.style.left = left + 'px'; this.$refs.previewImage.style.top = top + 'px'; }, }, }; </script> ``` 在上面的代码中,我们新增了一个图片预览的模态框,当用户点击图片时,会通过showPreview方法设置previewVisible属性为true,从而显示模态框。模态框中使用了Element UI的Slider组件,用于控制图片的缩放比例;同时还提供了向左旋转、向右旋转的按钮,用于调整图片的旋转角度;通过鼠标拖拽实现图片的平移功能。 注意,上面的示例只是一个简单的实现,还有很多细节需要注意,比如图片的边界判断、拖拽的时候阻止选中文本等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值