前言

骨架屏, 用于在数据还没有加载完成前期,为页面添加的框架结构,代替以前的loading,解决长时间白屏的尴尬

技术调研

  1. react-content-loader / vue-content-loader( 自行搜索)
  2. puppeteer + page-skeleton-webpack-plugin (饿了么内部)

特点: 可以结合webpack,动态生成骨架屏,目前了解到饿了么采用此方法,具体还没有做demo测试,感兴趣的可以先看看作者对此方法的总结 https://github.com/Jocs/jocs.github.io/issues/22

react-content-loader 使用

“description”: “SVG component to create placeholder loading, like Facebook cards loading.”,
通过svg创建的占位loading, 类似于facebook的cards loading

特点(官方)

  • 随意更改颜色,速度,尺寸甚至RTL ;
  • 有许多预设可供使用,请参阅示例 ;
  • 使用create-content-loader轻松创建自己的自定义加载器;(可以自定义加载器)
  • 只有1.4kB gzip和0依赖 ;

    安装

  1. npm方式安装
  2. npm i react-content-loader --save
  3. yarn方式安装
  4. yarn add react-content-loader

使用方法

  1. 可以自己创建骨架模版
  2. 采用内置的骨架类型(Facebook, Instagram, Code, List, BulletList)

简单demo

  1. import ContentLoader, {Facebook} from 'react-content-loader'
  2. /**
  3. *
  4. * 直接使用默认
  5. */
  6. const MyLoader = ()=><ContentLoader />
  7. const MyFaceBook = ()=> <FaceBook />
  8. /**
  9. * 可以自己创建
  10. */
  11. const MyLoad = ()=>{
  12. return <ContentLoader>
  13. {/*SVG shapes*/}
  14. <rect x='0' y='0'rx='5' ry='5' width='70' height='70'/>
  15. <rect x='80' y='17' rx='4' ry='4' width='300' height='13' />
  16. <rect x='80' y='40' rx='3' ry='3' width='250' height='10' />
  17. </ContentLoader>
  18. }

Options(选项设置)

  1. animate?: boolean 动画效果,默认为true,
  2. ariaLabel?: string | boolean 默认为Loading interface… 它用于描述它是什么元素,

使用false删除

  1. baseUrl?: string 如果您使用的, 默认为空字符串。这个prop常用作:<ContentLoader baseUrl={window.location.pathname} />它将用相对路径填充SVG属性
  2. speed?: number 默认为2。动画速度以秒为单位。
  3. interval?: number 默认为0.25。动画运行之间的时间间隔,作为动画速度的一小部分。
  4. className?: string 默认为空字符串。类名将在<svg />元素中设置。
  5. width?:number 默认为400。它将在视图框中设置attr <svg />。
  6. height?number 默认为130。它将在视图框中设置attr <svg />。
  7. gradientRatio?number 默认为2。动画渐变的宽度作为视框宽度的一部分。
  8. rtl? boolean 默认为false。内容从右到左。
  9. preserveAspectRatio?: string 默认为xMidYMid meet。宽高比选项<svg/>
  10. primary?:string 默认值#f3f3f3用作动画的背景。
  11. secondaryColor?: string 默认值#ecebeb用作动画的占位符/图层。
  12. primaryOpacity?: string 默认为1。背景不透明度(0 =透明,1 =不透明)用于解决Safari中的问题
  13. secondaryOpacity?:string 默认为1。动画不透明度(0 =透明,1 =不透明)用于解决Safari中的问题
  14. style?: React.CSSProperties 默认为空对象。
  15. uniquekey?: string 默认为随机唯一ID。使用相同的prop键值,这将解决SSR上的不一致问题

官网demo