H5骨架屏实战
H5骨架屏实战
- 骨架屏可提升用户体验,在网络情况比较差的时候有比较好的体验。看骨架屏总比白屏舒服。
- 骨架屏可以理解为是:当数据还未加载进来前,页面的一个空白版本 ,一个简单的关键渲染路径。
vue-skeleton-webpack-plugin
安装
npm install vue-skeleton-webpack-plugin
配置webpack
- 在 webpack.dev.conf.js, webpack.prod.conf.js 中分别添加配置
const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin')
const nodeExternals = require('webpack-node-externals')
plugins: [
...
new SkeletonWebpackPlugin({
webpackConfig: {
target: 'node',
devtool: false,
entry: {
// 入口
app: path.join(__dirname, '../src/entry-skeleton.js')
},
output: Object.assign({}, baseWebpackConfig.output, {
libraryTarget: 'commonjs2'
}),
// css 分离,这样骨架屏组中的style才能正常显示
externals: nodeExternals({
whitelist: /\.css$/
}),
plugins: []
},
quiet: true
}),
...
]
编码
- entry-skeleton.js 为骨架屏的入口配置文件
import Vue from 'vue'
import Skeleton from './Skeleton'
export default new Vue({
components: {
Skeleton
},
template: '<skeleton />'
})
- Skeleton.vue 是骨架屏界面,界面可由美工直接切好
<template>
<div class="skeleton-wrapper" id="skeleton_wrapper_id">
<section class="skeleton-block" style="text-align: center; margin-top: 30px;">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXcAAACqCAYAAABWFw+7AAAAAXNSR0IArs4c6QAACLFJREFUeAHt2kFKI00YBmA7ARPBM3gBYQ7hQvAebmY7B5mduPEeggsPMeAFPINgIiT5v2JmMQ7p1N9aSXXh4yaxq636fL7Ka6fN0ZEvAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgTGLtCNvUD1/RZYLpfnq9Xquuu6yzhyttlsTtkQqCkQe/El1n+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gBAgQIHEYgZXHK5L7Vur6BvuMx4fFisbiPx4u+cxwnQIAAgf0JxFX743w+v4rHt75VBl25p0nSZGnSeHqT3hb0Tew4AQIECJQV+JO5N7lgT6sOvnL/u9Tlcnm+Wq2uY8HLOH4WV/Onf497ToAAAQKfE4h8TR9Bf458fUj32HfdivncSn6aAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECX0LgP5aKKUy6TGDvAAAAAElFTkSuQmCC" style="width: 325px;">
</section>
</div>
</template>
<script>
export default {
name: 'skeleton'
}
</script>
<style scoped>
</style>
优化渲染
/* eslint-disable no-new */
const app = new Vue({
router,
components: { App },
template: '<App/>'
})
// 如果 JS 晚于 CSS 加载完成,那直接执行渲染
if (process.env.NODE_ENV === 'production') {
if (window.STYLE_READY) {
app.$mount('#app')
}
} else {
app.$mount('#app')
}
- 效果图
本文来自博客园,作者:struggle_time,转载请注明原文链接:https://www.cnblogs.com/songliquan/p/12784597.html