uniapp image 二次封装,新增loading和淡入效果

该博客介绍了如何针对uni-app的image组件进行二次封装,以实现图片加载时的loading效果和淡入过渡效果。通过新增参数如showLoading、loadingColor,用户可以自定义loading图标颜色,并在图片加载完成后平滑淡入显示,提高了用户体验。
摘要由CSDN通过智能技术生成

在使用uniapp image 组件的时候会发现图片组件没有loading的效果,并且加载进来的图片没有过度效果,给人的根据比较生硬,针对这个问题对image组件做了二次封装。

开放自定义参数:

  • src  图片路径信息
  • mode 与uniapp一致
  • style  图片自定义样式
  • showLoading 是否显示loading效果
  • loadingColor loading icon 颜色  
/**
* uniapp-image 二次封装
* 1.[新增] loading 效果
* 2.[新增] 淡入效果,之前uniapp-image展示有点生硬
*/
<template>
	<view class="f_image_content">
		<view class="title_image">
			<image :class="loading==true?'load-fade':''" :style="style" @load="loadingSuccess" :src="src" :mode="mode">
			</image>
			<view class="loading_icon" v-if="showLoading && !loading">
				<view class="circle" :style="formatLoadingColor"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// image address
			src: {
				type: String,
				default: ''
			},
			mode: {
				type: String,
				default: 'widthFix'
			},
			// image css
			style: {
				type: String,
				default: 'width: 100%;'
			},
			// show loading
			showLoading: {
				type: Boolean,
				default: true
			},
			// loading icon color
			loadingColor: {
				type: String,
				default: 'red'
			}
		},
		computed: {
			formatLoadingColor() {
				return 'border-top-color:' + this.loadingColor;

			}
		},
		name: "f-image",
		data() {
			return {
				loading: false,
			};
		},
		methods: {
			loadingSuccess() {
				this.loading = true;
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 淡入效果
	.load-fade {
		opacity: 1;
		animation: fade 1s linear normal;
	}

	@keyframes fade {
		from {
			opacity: 0
		}

		to {
			opacity: 1
		}
	}

	.title_image {
		display: flex;
		justify-content: center;
		position: relative;

		.loading_icon {
			position: absolute;
			top: 50%;
		}
	}

	.circle {
		box-sizing: border-box;
		width: 50upx;
		height: 50upx;
		display: inline-block;
		border: 5upx solid #f3f3f3;
		border-top: 5upx solid;
		border-radius: 50%;
		/* 动画旋转效果 */
		animation: rotate-360 1s infinite linear;
	}

	@keyframes rotate-360 {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值