自定义uni-app图片预览组件

文章展示了如何在uni-app中不满意uni-previewImage组件的功能而自定义一个图片预览组件cu-previewImg。该组件使用Vue.js编写,包括模板、脚本和样式部分,支持设置图片宽度、高度和路径,并可直接引用到项目中进行图片预览。
摘要由CSDN通过智能技术生成

图片预览组件在uni-app官网uni-previewImage实现的但是感觉他的这个满足不了自己的需求,就自己写了个

1.定义公共组件cu-previewImg.vue

<template>
	<view class="">
		<view class="img-box" :style="`width: ${width}rpx;height: ${height}rpx;`" @click="show = !show">
			<img :src="src" alt="" style="width: 100%; height: 100%;">
		</view>
		<view class="previewImg" v-if="show" @click="show = !show">
			<img :src="src" alt="" class="previewImg-img">
		</view>
	</view>
</template>

<script>
	/**
      *height 所展示图片的高度
	  * width 所展示图片的宽度
	  * src 所访问图片的路径
	 */
	export default {
		props: {
			width: {
				type: [Number,String],
				default: 10
			},
			height: {
				type: [Number,String],
				default: 10
			},
// 这里有默认图片可以定义默认图片地址
			src: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				show: false
			}
		},
		methods: {
		}
	}
</script>

<style lang="scss" scoped>
	.previewImg{
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		    background-color: rgba(0, 0, 0, 0.6);
		    z-index: 10073;
		    transition: all 0.25s ease-in-out 0s;
		    transform: scale(1, 1);
			&-img {
				width: 50vh;
				height: 50%;
			}
	}
</style>

2.直接在想要展示的位置引用该组件就Ok了

 <cu-previewImg :src="form.imgUrl"   width="78.13" height="78.13"></cu-previewImg>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值