项目背景

会员首页是整个会员的承载页,项目中集合了会员身份、金币、任务、cps、各种弹窗(至少8个),整个页面还要支持运营日氛围配置。

项目存在的问题

随着版本的迭代,html引入资源越来越多,项目体积越来越大,页面加载速度越来越慢,白屏时间增长。

优化目标

减少白屏时间,提要用户留存率

技术实现

1. 根据页面样式生成骨架图,并转换成base64

  • 将图片转换成base64的目的是减少网络请求,html加载完之后直接渲染
  • 骨架图的作用是让用户看到的白屏变成一个页面的大致骨架,让用户有一种已经进入了页面感觉

2. 在html中加入骨架占位元素

    /*骨架图元素css样式*/.main-bone {position: absolute;left: 0;top: 0;width: 100%;height: 100%;background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAAWuAgMAAABGjE+7AAAADFBMVEX19fX////8/Pz4+Pjt9JwuAAAItUlEQVR42uzOsQ0CQQAEsSuRhH4onQSJdLLfk+wKfN73esk38vIL5CN5+QXykbz8AvlIXn6BfCQvv0A+kpdfIB/Jyy+Qf8g//zk3+uXPleQjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+QjefkF8pG8/AL5SF5+gXwkL79APpKXXyAfycsvkI/k5RfIR/LyC+Qj+S97d2wbRRSFUfgaywEBInJO4hIITOQSCPwMAQEFOKAEl+DEVUARboImiCgBhBbN7HtzzZUIfPbtOen+kj/h3Z317IwQT0h8MfHiCYkvJl48IfHFxIsnJL6YePGExBcTL56Q+GLixRMSX0y8eELii4kXT0j8s7Tg7+IQa4fbe/G1xIsnJL6YePGExBcTL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g4ReCj8HDLwQfg4dfCD4GD78QfAwefiH4GDz8QvAxePiF4GPw8AvBx+DhF4KPwcMvBB+Dh18IPgYPvxB8DB5+IfgY/G/4DxQS+JpNaUXfAAAAAElFTkSuQmCC');background-color: #fff;background-size: cover;z-index: 1000;transition: all .6s linear;-webkit-transition: all .6s linear;pointer-events: none;
    }复制代码
<!--增加占位--><div id="home_skeleton" class="main-bone" style="display: none"></div>复制代码

3. 增加多页支持

<!--
    1、basename 是对应着Router的basename
    2、根据页面路由匹配是否显示对应的骨架,如果是单一的页面可以把下面逻辑简化,去掉for循环检索
--><script>var pathname = window.location.pathname, hash = window.location.hash, basename = '/member/memberCenter',
        skeletons = [{id: "home_skeleton", el: document.querySelector("#home_skeleton")}], isMatched = function (e, t) {return "hash" === t ? e.test(hash.replace("#", "")) : "history" === t && e.test(pathname.replace(basename, ''))
        }, showSkeleton = function (e) {for (var t = 0; t < skeletons.length; t++) {var o = skeletons[t];
                e === o.id ? o.el.style = "display:block;" : o.el.style = "display:none;"}
        }, removeChild = function () {for (var t = 0; t < skeletons.length; t++) {var o = skeletons[t];if (o && o.el) {document.body.removeChild(o.el);
                }
            }
        };    //要显示骨架图的正则匹配var match_skeleton_route=/^\/index.html/;//匹配当前路由是否需要显示骨架图isMatched(match_skeleton_route, "history") ? showSkeleton("home_skeleton") : removeChild();</script>复制代码

4. 在页面渲染完之后,手动将其隐藏掉

手动关闭的好处是,保证从骨架图到页面首屏渲染完切换时比较顺滑

    /**
     * 关闭骨架屏
     */closeSkeleton = () => {const skeleton = document.getElementById("home_skeleton");if (skeleton) {//透明度设置为0,执行过渡动画,动画过渡事件必须小于延迟时间,否则动画看起来不完整skeleton.style.opacity = '0';setTimeout(() => {//延迟1秒之后删除骨架图占位元素const tempSkeleton = document.getElementById("home_skeleton");if (tempSkeleton) {document.body.removeChild(tempSkeleton);
                }
            }, 1000);
        }
    }复制代码

tips: 白屏周期只能控制html渲染完成 到 页面整体渲染完这段时间,html下载过程的白屏问题无法控制,如果非要操作可能需要app给个默认的加载图