vue3 element-plus 图片预览

一、效果图

hover 点击预览

二、代码

<template>
	<div class="box" @click="emits('change')" :style="{ width: size + 'px', height: size + 'px' }">
		<el-image :src="props.src" fit="scale-down" :style="{ width: size + 'px', height: size + 'px' }">
			<!-- 图片加载失败中定义展示内容 -->
			<!-- <template #error>
				<div class="flex align-middle">
					<el-icon><Picture /></el-icon>
				</div>
			</template> -->
		</el-image>
		<div class="mask">
			<el-icon :size="20" @click="open" class="icon">
				<ZoomIn />
			</el-icon>
		</div>
	</div>
	<el-image-viewer v-if="dialogVisible" :url-list="[props.src]" hide-on-click-modal teleported close-on-press-escape @close="handleClose" />
</template>

<script setup lang="ts">
const props = defineProps({
	src: {
		type: String,
		default: '',
	},
	size: {
		type: String,
	},
});
const emits = defineEmits(['change', 'update:modelValue']);
const dialogVisible = ref(false);
const open = () => {
	dialogVisible.value = true;
};
const handleClose = () => {
	dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.box {
	position: relative;
	border-radius: 7px;

	.mask {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		display: none;
		background: rgba(58, 58, 58, 0.5);
		width: 100%;
		height: 100%;
		color: #fff;
		justify-content: center;
		align-items: center;
		border-radius: 7px;
		.icon {
			cursor: pointer;
		}
	}
	&:hover {
		.mask {
			display: flex;
		}
	}
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值