设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间13 现在时间13 缓存数据 到现在一共是295天,有了人生中第一张迷你专辑,我期许自己这不会是句号,只会是个逗号,会一直一直一直突破的,直到我唱不动的那天。

到现在一共是295天,有了人生中第一张迷你专辑,我期许自己这不会是句号,只会是个逗号,会一直一直一直突破的,直到我唱不动的那天。 -- 一种原谅

查看: 894|回复: 2

基于React实现下拉刷新效果

[复制链接]

  离线 

TA的专栏

  • 打卡等级:即来则安
  • 打卡总天数:15
  • 打卡月天数:0
  • 打卡总奖励:225
  • 最近打卡:2023-08-27 06:16:20
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
51
主题
41
精华
0
金钱
375
积分
102
注册时间
2023-8-12
最后登录
2025-5-31

发表于 2024-9-11 20:54:38 | 显示全部楼层 |阅读模式
目录


  • 简介
  • 实现效果
  • 具体代码

    • 布局 & 逻辑
    • 使用demo

  • 番外:上拉加载实现

简介

本文基于react实现下拉刷新效果,在下拉的时候会进入loading状态。

实现效果

1.gif

效果如上图所示,在下拉到底部时候,会出现loading条,在处理完成后loading条消失。

具体代码


布局 & 逻辑
  1. import {useRef, useState} from "react";

  2. export const ScrollView = ({loadingComponent, contentComponent}) => {
  3.     const LoadingComponent = loadingComponent;
  4.     const ContentComponent = contentComponent;

  5.     /**
  6.      * 加载状态
  7.      */
  8.     const [loading, setLoading] = useState(false);

  9.     /**
  10.      * 滚动容器引用
  11.      */
  12.     const scrollRef = useRef(null);

  13.     let contentStyle = {height: '30px', width:'100%', textAlign:'center', display:'none'};

  14.     if (loading){ // 加载中显示
  15.         contentStyle = {height: '30px', width:'100%', textAlign:'center'};
  16.         scrollRef.current.scrollTop = 0; // 滚到头部
  17.     }

  18.     const handleScroll = ()=>{
  19.         const {scrollTop} = scrollRef.current;

  20.         if (scrollTop > 50 && !loading){
  21.             setLoading(true); // 设置为加载中状态

  22.             // 模拟数据加载
  23.             setTimeout(()=>{
  24.                 setLoading(false); // 加载完成
  25.             }, 3000)
  26.         }

  27.     }
  28.     return (
  29.         <div style={{height: '200px', overflow:'auto', width:'40%'}} ref={scrollRef} onScroll={handleScroll}>
  30.             <div style={contentStyle}>
  31.                 <LoadingComponent/>
  32.             </div>
  33.             <div style={{height:'300px', width:'100%'}}>
  34.                 <ContentComponent/>
  35.             </div>
  36.         </div>
  37.     )
  38. }
复制代码
使用demo

  1. import {ScrollView} from "./component/scroll-view/ScrollView";

  2. const App = ()=> {

  3.     return (
  4.        <ScrollView loadingComponent={Loading} contentComponent={Content}>
  5.        </ScrollView>
  6.     )
  7. }
  8. const Loading = ()=>{
  9.     return <div>loading</div>
  10. }
  11. const Content  = ()=>{
  12.     return <div> hello, world</div>
  13. }



  14. export default App;
复制代码
番外:上拉加载实现

1、下载react-pullload
  1. npm i react-pullload
复制代码
2、在组件中去引用
  1. import ReactPullLoad,{ STATS } from "react-pullload";
复制代码
3、css样式
①引用插件内的样式
  1. import "node_modules/react-pullload/dist/ReactPullLoad.css";
复制代码
②或者直接引入使用下列代码:  
  1.     .pull-load {

  2.                       position: relative;

  3.                       overflow-y: scroll;

  4.                       -webkit-overflow-scrolling: touch;

  5.             }

  6.             .pull-load-head {

  7.                       position: absolute;

  8.                       transform: translate3d(0px, -100%, 0px);

  9.                       width: 100%;

  10.             }

  11.             .state-refreshing .pull-load-head,

  12.             .state-refreshed .pull-load-head {

  13.                       position: relative;

  14.                       transform: none;

  15.             }

  16.             .pull-load-body {

  17.                       position: relative;

  18.             }

  19.             .state-refreshing .pull-load-body {

  20.                       transition: transform 0.2s;

  21.             }

  22.             .state-reset .pull-load-body {

  23.                       transition: transform 0.2s;

  24.             }

  25.             .pull-load-head-default {

  26.                       text-align: center;

  27.                       font-size: 12px;

  28.                       line-height: 0.8rem;

  29.                       color: #7676a1;

  30.             }

  31.             .state-pulling .pull-load-head-default:after {

  32.                       content: '下拉刷新';

  33.             }

  34.             .state-pulling.enough .pull-load-head-default:after {

  35.                       content: '松开刷新';

  36.             }

  37.             .state-refreshing .pull-load-head-default:after {

  38.                       content: '正在刷新...';

  39.             }

  40.             .state-refreshed .pull-load-head-default:after {

  41.                       content: '刷新成功';

  42.             }

  43.             .state-pulling .pull-load-head-default {

  44.                       opacity: 1;

  45.             }

  46.             .state-pulling .pull-load-head-default i {

  47.                   display: inline-block;

  48.                   font-size: 0.3rem;

  49.                   margin-right: .6em;

  50.                   margin-top: -3px;

  51.                   vertical-align: middle;

  52.                   height: 0.3rem;

  53.                   border-left: 1px solid;

  54.                   position: relative;

  55.                   transition: transform .3s ease;

  56.             }

  57.             .state-pulling .pull-load-head-default i:before,

  58.             .state-pulling .pull-load-head-default i:after {

  59.                   content: '';

  60.                   position: absolute;

  61.                   font-size: .5em;

  62.                   width: 1em;

  63.                   bottom: 0px;

  64.                   border-top: 1px solid;

  65.             }

  66.             .state-pulling .pull-load-head-default i:before {

  67.                   right: 1px;

  68.                   transform: rotate(50deg);

  69.                   transform-origin: right;

  70.             }

  71.             .state-pulling .pull-load-head-default i:after {

  72.                   left: 0px;

  73.                   transform: rotate(-50deg);

  74.                   transform-origin: left;

  75.             }

  76.             .state-pulling.enough .pull-load-head-default i {

  77.                   transform: rotate(180deg);

  78.             }

  79.             .state-refreshing .pull-load-head-default i {

  80.                   margin-right: 10px;

  81.                   margin-top: -3px;

  82.                   display: inline-block;

  83.                   vertical-align: middle;

  84.                   font-size: 0.3rem;

  85.                   width: 0.3rem;

  86.                   height: 0.3rem;

  87.                   border: 2px solid #9494b6;

  88.                   border-top-color: #fff;

  89.                   border-radius: 100%;

  90.                   animation: circle .8s infinite linear;

  91.             }

  92.             .state-refreshed .pull-load-head-default {

  93.                   opacity: 1;

  94.                   transition: opacity 1s;

  95.             }

  96.             .state-refreshed .pull-load-head-default i {

  97.                 display: inline-block;

  98.                 box-sizing: content-box;

  99.                 vertical-align: middle;

  100.                 margin-right: 10px;

  101.                 margin-top: -3px;

  102.                 font-size: 14px;

  103.                 height: 1em;

  104.                 width: 1em;

  105.                 border: 2px solid;

  106.                 border-radius: 100%;

  107.                 position: relative;

  108.          }

  109.          .state-refreshed .pull-load-head-default i:before {

  110.                content: '';

  111.               position: absolute;

  112.               top: -2px;

  113.               left: 4px;

  114.               height: 11px;

  115.               width: 5px;

  116.               border: solid;

  117.               border-width: 0 2px 2px 0;

  118.               transform: rotate(40deg);

  119.         }

  120.         .pull-load-footer-default {

  121.               text-align: center;

  122.               font-size: 12px;

  123.               line-height: 0.8rem;

  124.               color: #7676a1;

  125.        }

  126.         .state-loading .pull-load-footer-default:after {

  127.               content: '加载更多';

  128.         }

  129.         .pull-load-footer-default.nomore:after {

  130.               content: '没有更多';

  131.         }

  132.         .state-loading .pull-load-footer-default i {

  133.               margin-right: 10px;

  134.               margin-top: -3px;

  135.               display: inline-block;

  136.               vertical-align: middle;

  137.               font-size: 0.3rem;

  138.               width: 0.3rem;

  139.               height: 0.3rem;

  140.               border: 2px solid #9494b6;

  141.               border-top-color: #fff;

  142.               border-radius: 100%;

  143.               animation: circle .8s infinite linear;

  144.         }

  145.         @keyframes circle {

  146.              100% {

  147.                     transform: rotate(360deg);

  148.               }

  149.         }
复制代码
4、pullLoad标签包裹
  1. <ReactPullLoad

  2.                     downEnough={150}

  3.                     ref="reactpullload"

  4.                     className="block"

  5.                      *加上下面注释的属性会出问题

  6.                     // isBlockContainer={true}

  7.                     action={this.state.action}

  8.                     handleAction={this.handleAction}

  9.                     hasMore={this.state.hasMore}

  10.                     style={{paddingTop: 132}}

  11.                     distanceBottom={1000}>

  12.                     .....  this is list

  13.                     </ReactPullLoad>
复制代码
5、scroll函数
  1. constructor(props) {

  2.         super(props);

  3.         this.state = {

  4.               // scroll 相关

  5.               hasMore: true,

  6.               action: STATS.init,

  7.         }

  8.     }

  9. // 滚动条

  10.     handleAction = (action) => {

  11.       if(action === this.state.action ||

  12.         action === STATS.refreshing && this.state.action === STATS.loading ||

  13.         action === STATS.loading && this.state.action === STATS.refreshing){

  14.         return false

  15.       }

  16.       if(action === STATS.refreshing){//刷新

  17.           setTimeout(()=>{

  18.                   //refreshing complete

  19.                 下拉刷新

  20.           }, 1000)

  21.       } else if(action === STATS.loading){//加载更多

  22.         this.setState({

  23.           hasMore: true

  24.         });

  25.         setTimeout(()=>{

  26.                   if(this.state.index === this.state.curPage){

  27.                          this.setState({

  28.                                  action: STATS.reset,

  29.                                   hasMore: false

  30.                            });

  31.                         } else{

  32.                                     上拉加载....

  33.                           }

  34.             }, 1000)

  35.       }

  36.       this.setState({

  37.         action: action

  38.       })

  39.     }
复制代码
到此这篇关于基于React实现下拉刷新效果的文章就介绍到这了,更多相关React下拉刷新内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
11
积分
2
注册时间
2024-8-26
最后登录
2024-8-26

发表于 2024-12-27 12:26:41 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
12
积分
4
注册时间
2023-10-27
最后登录
2023-10-27

发表于 3 天前 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表