实现react移动端渐变骨架屏简单案例

12 篇文章 0 订阅

骨架屏是APP和移动端h5必备的东西,以下,我提供2个骨架屏的设计思路:

一、如果是SPA单页应用,比如react、vue、react-native、angular

一般在index.html中会有一个根节点

<div id="root"></div>

第一个方案就是在这个div中写死静态的html+css骨架屏代码,这也是我所推荐的,灵活性虽然不足,但是用户体验非常好,以react来举例,在数据没有被react-dom接管之前(接管后会替换掉根节点内的内容),立刻就可以被浏览器渲染展示,而不用等js执行

下面是完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="theme-color" content="#000000">
  <!-- 启用浏览器的极速模式(webkit) -->
  <meta name="renderer" content="webkit">
  <!-- 默认宽度为设备宽度,缩放大小为 1,禁止缩放 -->
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover">
  <!-- 忽略页面中的数字识别为电话,忽略email识别 -->
  <meta name="format-detection" content="telphone=no, email=no"/>
  <link rel="shortcut icon" href="<%= htmlWebpackPlugin.files.publicPath %>favicon.png">
  <link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath %>manifest.json">
  <link rel="stylesheet" href="<%= htmlWebpackPlugin.files.publicPath %>fonts_cms/iconfont.css">
  <title>公司信息-xxxx</title>
  <style>
    /* 配置 rem */
    html { font-size: calc(100vw / 26.8); }
    body { margin: 8px 0 }
  </style>
  <style>
    .skeleton_wrap {
      margin-top: 76px;
      padding: 0 15px 1.5rem 15px;
    }

    .linearGradient {
      position: absolute;
      z-index: 100;
      top: 3.144rem;
      width: 100%;
      height: calc(100vh - 44px);
      background-image: linear-gradient(
        top,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 80%,
        rgba(255, 255, 255, 1) 100%
      );
      background-image: -o-linear-gradient(
        top,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 80%,
        rgba(255, 255, 255, 1) 100%
      );
      background-image: -moz-linear-gradient(
        top,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 80%,
        rgba(255, 255, 255, 1) 100%
      );
      background-image: -webkit-linear-gradient(
        top,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 80%,
        rgba(255, 255, 255, 1) 100%
      );
      background-image: -ms-linear-gradient(
        top,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 80%,
        rgba(255, 255, 255, 1) 100%
      );
    }

    .container {
      padding: 1.3rem;
      /* margin-top: 0.857rem; */
    }

    .wrapper {
      /* height: 100vh; */
      width: 100%;
      margin-bottom: 2.144rem;
      /* background-color: green; */
    }
    .title_100 {
      width: 100%;
      height: 1.858rem;
      margin-bottom: 1.07rem;
      background-color: rgba(0, 0, 0, 0.1);
    }
    .title_45 {
      width: 45%;
      height: 1.858rem;
      margin-bottom: 1.07rem;
      background-color: rgba(0, 0, 0, 0.1);
    }

    .paragraph_100 {
      width: 100%;
      /* height: 1rem;
      margin-bottom: 1.07rem; */
      background-color: rgba(0, 0, 0, 0.1);

      height: 10px;
      margin-bottom: 10px;
    }
    .paragraph_85 {
      width: 85%;
      /* height: 1rem;
      margin-bottom: 1.07rem; */
      background-color: rgba(0, 0, 0, 0.1);

      height: 10px;
      margin-bottom: 10px;
    }
    .paragraph_40 {
      width: 40%;
      /* height: 1rem;
      margin-bottom: 1.07rem; */
      background-color: rgba(0, 0, 0, 0.1);

      height: 10px;
      margin-bottom: 10px;
    }

    .imageContent_wrap {
      flex: 1;
      flex-direction: row;
      justify-content: space-between;
      align-items: flex-start;
    }

    .imageContent_circle {
      display: inline-block;
      /* width: 3.573rem;
      height: 3.573rem; */
      width: 30px;
      height: 30px;
      
      margin-right: 1.07rem;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.1);
    }

    .imageContent_column {
      display: inline-block;
      width: 50%;
      /* height: 3.573rem; */
      height: 30px;
    }

    .imageContent_content {
      width: 80%;
      /* height: 1rem; */
      /* margin-bottom: 1.07rem; */
      background-color: rgba(0, 0, 0, 0.1);

      height: 10px;
      margin-bottom: 10px;
    }

    .imageContent_content_no_margin {
      margin-bottom: 0rem;
    }

    .animOpacityAni {
      animation: opacityAni 1s 0s infinite;
      -webkit-animation: opacityAni 1s 0s infinite;
      -moz-animation: opacityAni 1s 0s infinite;
    }

    @keyframes opacityAni {
      0% {
        opacity: 0.2;
      }
      50% {
        opacity: 0.8;
      }
      100% {
        opacity: 0.2;
      }
    }
    @-webkit-keyframes opacityAni {
      0% {
        opacity: 0.2;
      }
      50% {
        opacity: 0.8;
      }
      100% {
        opacity: 0.2;
      }
    }
    @-moz-keyframes opacityAni {
      0% {
        opacity: 0.2;
      }
      50% {
        opacity: 0.8;
      }
      100% {
        opacity: 0.2;
      }
    }

  </style>
</head>
<body>
  <div id="root">
    <div class="skeleton_wrap">
      <!-- 标题部分 -->
      <div class="wrapper animOpacityAni">
        <div class="title_100"></div>
        <div class="title_45"></div>
      </div>
      <!-- 图片+标题 -->
      <div
        class="wrapper animOpacityAni imageContent_wrap"
      >
        <div class="imageContent_circle"></div>

        <div class="imageContent_column">
          <div
            class="imageContent_content"
          ></div>
          <div
            class="imageContent_content imageContent_content_no_margin"
          ></div>
        </div>
      </div>
      <!-- 短段落 -->
      <div>
        <div class="paragraph_100"></div>
        <div class="paragraph_100"></div>
        <div class="paragraph_85"></div>
      </div>
      <!-- 中段落 -->
      <div>
        <div class="paragraph_100"></div>
        <div class="paragraph_85"></div>
        <div class="paragraph_100"></div>
        <div class="paragraph_85"></div>
      </div>
      <!-- 长段落 -->
      <div>
        <div class="paragraph_100"></div>
        <div class="paragraph_85"></div>
        <div class="paragraph_100"></div>
        <div class="paragraph_85"></div>
        <div class="paragraph_40"></div>
      </div>
    </div>
      
  </div>
</body>
</html>

 

二、接下来是第二种方案

以组件的思想进行组件封装📦,但是在详情数据请求回来,react执行js进行切换内容时,会有一定的时间花销,会有一段时间的白屏,性能不够好 

首先是最终效果图(取截图,真实效果是会忽隐忽现)

调用各部分骨架的Skeleton.js

import React from 'react';
import moment from 'moment';
import styles from './skeleton.css';
import Title from './Title';
import Paragraph from './Paragraph';
import ImageContent from './ImageContent';

export default class extends React.Component {
  state = {};

  componentDidMount() {}

  render() {
    const largeSize = 30;
    const Size = 10;
    return (
      <div>
        <div className={styles.linearGradient}></div>
        <div className={styles.container}>
          <Title />
          <ImageContent
            circleStyle={{ width: largeSize, height: largeSize }}
            descStyle={{ height: largeSize }}
            columnItemStyle={{ height: Size, marginBottom: 10 }}
          />
          <Paragraph type="small" rowStyle={{ height: Size, marginBottom: Size }} />
          <Paragraph
            type="middle"
            rowStyle={{ height: Size, marginBottom: Size }}
          />
          <Paragraph type="large" rowStyle={{ height: Size, marginBottom: Size }} />
        </div>
      </div>
    );
  }
}

skeleton.css

.linearGradient {
  position: absolute;
  z-index: 100;
  top: 44px;
  width: 100%;
  height: calc(100vh - 44px);
  background-image: linear-gradient(
    top,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 1) 80%,
    rgba(255, 255, 255, 1) 100%
  );
  background-image: -o-linear-gradient(
    top,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 1) 80%,
    rgba(255, 255, 255, 1) 100%
  );
  background-image: -moz-linear-gradient(
    top,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 1) 80%,
    rgba(255, 255, 255, 1) 100%
  );
  background-image: -webkit-linear-gradient(
    top,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 1) 80%,
    rgba(255, 255, 255, 1) 100%
  );
  background-image: -ms-linear-gradient(
    top,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 1) 80%,
    rgba(255, 255, 255, 1) 100%
  );
}

.container {
  padding: 20px;
  margin-top: 12px;
}

.wrapper {
  /* height: 100vh; */
  width: 100%;
  margin-bottom: 30px;
  /* background-color: green; */
}
.title_100 {
  width: 100%;
  height: 26px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}
.title_45 {
  width: 45%;
  height: 26px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}

.paragraph_100 {
  width: 100%;
  height: 14px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}
.paragraph_85 {
  width: 85%;
  height: 14px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}
.paragraph_40 {
  width: 40%;
  height: 14px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}

.imageContent_wrap {
  flex: 1;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
}

.imageContent_circle {
  display: inline-block;
  width: 50px;
  height: 50px;
  margin-right: 15px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.1);
}

.imageContent_column {
  display: inline-block;
  width: 50%;
  height: 50px;
}

.imageContent_content {
  width: 80%;
  height: 14px;
  margin-bottom: 15px;
  background-color: rgba(0, 0, 0, 0.1);
}

.imageContent_content_no_margin {
  margin-bottom: 0px;
}

.animOpacityAni {
  animation: opacityAni 1s 0s infinite;
  -webkit-animation: opacityAni 1s 0s infinite;
  -moz-animation: opacityAni 1s 0s infinite;
}

@keyframes opacityAni {
  0% {
    opacity: 0.2;
  }
  50% {
    opacity: 0.8;
  }
  100% {
    opacity: 0.2;
  }
}
@-webkit-keyframes opacityAni {
  0% {
    opacity: 0.2;
  }
  50% {
    opacity: 0.8;
  }
  100% {
    opacity: 0.2;
  }
}
@-moz-keyframes opacityAni {
  0% {
    opacity: 0.2;
  }
  50% {
    opacity: 0.8;
  }
  100% {
    opacity: 0.2;
  }
}

Title.js

import React from 'react';
import moment from 'moment';
import styles from './skeleton.css';

export default class extends React.Component {
  state = {};

  componentDidMount() {}

  render() {
    return (
      <div className={`${styles.wrapper} ${styles.animOpacityAni}`}>
        <div className={styles.title_100}></div>
        <div className={styles.title_45}></div>
      </div>
    );
  }
}

Paragraph.js

import React from 'react';
import moment from 'moment';
import styles from './skeleton.css';

export default class extends React.Component {
  state = {};

  componentDidMount() {}

  judgeType() {
    const { type } = this.props;
    switch (type) {
      case 'small':
        return this.renderSmall();
        break;
      case 'middle':
        return this.renderMiddle();
        break;
      case 'large':
        return this.renderLarge();
        break;
    }
  }

  renderSmall() {
    const { rowStyle } = this.props;
    return (
      <div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_85} style={rowStyle}></div>
      </div>
    );
  }

  renderMiddle() {
    const { rowStyle } = this.props;
    return (
      <div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_85} style={rowStyle}></div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_85} style={rowStyle}></div>
      </div>
    );
  }

  renderLarge() {
    const { rowStyle } = this.props;
    return (
      <div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_85} style={rowStyle}></div>
        <div className={styles.paragraph_100} style={rowStyle}></div>
        <div className={styles.paragraph_85} style={rowStyle}></div>
        <div className={styles.paragraph_40} style={rowStyle}></div>
      </div>
    );
  }

  render() {
    const renderContent = this.judgeType();

    return (
      <div className={`${styles.wrapper} ${styles.animOpacityAni}`}>
        {renderContent}
      </div>
    );
  }
}

ImageContent.js

import React from 'react';
import moment from 'moment';
import styles from './skeleton.css';

export default class extends React.Component {
  state = {};

  componentDidMount() {}

  render() {
    const { circleStyle, descStyle, columnItemStyle } = this.props;

    return (
      <div
        className={`${styles.wrapper} ${styles.animOpacityAni} ${styles.imageContent_wrap}`}
      >
        <div className={styles.imageContent_circle} style={circleStyle}></div>

        <div className={styles.imageContent_column} style={descStyle}>
          <div
            className={styles.imageContent_content}
            style={columnItemStyle}
          ></div>
          <div
            className={`${styles.imageContent_content} ${styles.imageContent_content_no_margin}`}
            style={columnItemStyle}
          ></div>
        </div>
      </div>
    );
  }
}

如何使用

render() {
    const { navTitle, barVisible, data } = this.state;
    const { detail } = this.props;

    return (
      <React.Fragment>
        {!!data ? (
          <div>
            <App
              {...this.props}
              detail={data || detail}
              barVisible={barVisible}
              setNavTitle={navTitle => this.setState({ navTitle })}
              hideFontBar={() => this.toggleBarVisible(false)}
            />
          </div>
        ) : (
          <Skeleton />
        )}
      </React.Fragment>
    );
  }

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React移动端实现滑动懒加载可以使用react-infinite-scroll-component插件来实现。 安装插件: ``` npm install react-infinite-scroll-component ``` 使用: ``` import React, { Component } from 'react'; import InfiniteScroll from 'react-infinite-scroll-component'; class App extends Component { state = { items: Array.from({ length: 20 }), hasMore: true, }; fetchMoreData = () => { if (this.state.items.length >= 50) { this.setState({ hasMore: false }); return; } // 模拟异步加载数据 setTimeout(() => { this.setState({ items: this.state.items.concat(Array.from({ length: 20 })), }); }, 1500); }; render() { return ( <div> <InfiniteScroll dataLength={this.state.items.length} // 列表长度 next={this.fetchMoreData} // 触底时的回调函数 hasMore={this.state.hasMore} // 是否还有更多数据 loader={<h4>Loading...</h4>} // 加载时的提示 endMessage={<p>No more items</p>} // 列表到底时的提示 > {this.state.items.map((item, index) => ( <div key={index} style={{ padding: 20, border: '1px solid gray' }}> {`Item ${index}`} </div> ))} </InfiniteScroll> </div> ); } } export default App; ``` 在上面的代码中,我们使用了`react-infinite-scroll-component`插件,通过在`InfiniteScroll`组件中设置`dataLength`、`next`、`hasMore`、`loader`和`endMessage`等属性来实现滑动懒加载效果。当用户滑动到列表底部时,`next`函数将被调用,我们可以在该函数中异步加载更多数据,直到`hasMore`为`false`时停止加载。同时,在加载过程中,我们可以显示一个加载提示,当列表到达底部时,我们可以显示一个提示信息告诉用户已经没有更多数据可以加载了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hzxOnlineOk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值