• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

uniapp 小程序懒加载(自己封装组件)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

前言

用uniapp 开发小程序,商品的列表页面,当然需要用到懒加载了,

当然我写的这个拉加载 只是针对效果图,但是也很炫酷,适合列表哦!! 

啊哈

原理其实就是用了 

 @load 和error 的2个方法来判断是否加载完全 和出错

<image class="real-image" 
        @load="onLoaded" 
        :src="realSrc" 
        :mode="mode" 
        @error="handleImgError">
</image>

然后就是代码周小程序期逻辑了:

我封装了成了了组件,还是看代码吧,啊哈

LOOK:

<!-- 懒加载的loadImage -->
<template>
    <!-- mode="widthFix" -->
    <view class="lazy-image" :style="{\'width\': (width? width+\'rpx\':\'100%\')}">
        <image class="real-image" 
        @load="onLoaded" 
        :src="realSrc" 
        :mode="mode" 
        @error="handleImgError"></image>
        <view :class="loaded?\'loaded\':\'\'" v-if="!isLoadError">
            <image :src="placeholdSrc" mode="aspectFit"></image>
        </view>
        <view :class="loaded?\'loaded\':\'\'" v-if="isLoadError">
            <image :src="failSrc" mode="aspectFit"></image>
        </view>
    </view>
</template>

<script>
    export default {
        props:{
            placeholdSrc:{
                type:String,
                default:"../static/easyLoadimage/loading.gif" //loading.gif loadfail.png
            },
            failSrc:{
                type:String,
                default:"../static/easyLoadimage/loadfail.png" //
            },
            realSrc:{
                type:String,
                default:""
            },
            mode:{
                type:String,
                default:"widthFix"
            },
            width:{
                type:String,
                default:""
            }
        },
        data(){
            return {
                loaded:false,
                isLoadError:false,
                showImg:false,
            }
        },
        methods:{
            onLoaded(e){
                this.loaded = true;
                this.showImg = true
            },
            // 加载错误
            handleImgError(e) {
                //console.log(e)
                this.isLoadError = true;
            }
        },
        // 销毁代码
        beforeDestroy() {
            console.log(\'销毁\')
            this.loaded = false;
            this.isLoadError = false;
        }
    }
</script>

<style lang="scss" scoped>
    .lazy-image{
        height: 100%;
        width: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        position: relative;
        image{
            width: 100%;
        }
        view{
            background-color: #eee;
            position: absolute;
            z-index: 2;
            top: 0;
            left: 0;
            height: 100%;
            width: 100%;
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            transition: opacity 0.4s linear;
            image{
                width: 100%;
            }
        }
        .loaded{
            opacity: 0;
        }
    }
</style>

用 css3动画判读

isLoadError:false 这个熟悉来判断是否加载 然后用透明度来把加载的图片为0 显示对的图片地址
可以吧。。。

面对疾风吧,帮到你了 请点个赞把 啊哈

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序云开发数据懒加载发布时间:2022-07-18
下一篇:
微信小程序性能优化发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap