uniapp 实现图片加载组件默认占位

学习 · 2021-03-30
<template>
<view class="load-image" :style="{width: width + 'rpx', height: height + 'rpx' }">
    <image class="load-image-show" :style="{borderRadius: borderRadius + 'rpx'}"
         :class="{load: imgLoaded}" 
         :src="imgLoaded ? loadImage : $img('images/imagefail.png', { from: 'local' })"
         mode="aspectFill" 
         @load="imgLoaded = true" />
</view>
</template>

<script>
export default {
    name: "loadimage",
    props: {
        loadImage: String,
        width: [Number,String],
        height: [Number,String],
        borderRadius: {
            type: [Number,String],
            default : 8
        }
    },
    data() {
        return {
            imgLoaded: false
        };
    }
}
</script>

<style lang="scss">
.load-image-show {
    width: 100%;
    height: 100%;
    transition: opacity 0.6s ease-in;
    opacity: 0;
    &.load {
        opacity: 1;
    }
}
.load-image-default {
    width: 100%;
    height: 100%;
}
</style>

uniapp 图片
Theme Jasmine by Kent Liao | 桂ICP备15008025号-6