当前位置: 代码网 > it编程>编程语言>Javascript > 基于React实现下拉刷新效果

基于React实现下拉刷新效果

2024年05月18日 Javascript 我要评论
简介本文基于react实现下拉刷新效果,在下拉的时候会进入loading状态。实现效果效果如上图所示,在下拉到底部时候,会出现loading条,在处理完成后loading条消失。具体代码布局 &

简介

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

实现效果

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

具体代码

布局 & 逻辑

import {useref, usestate} from "react";
 
export const scrollview = ({loadingcomponent, contentcomponent}) => {
    const loadingcomponent = loadingcomponent;
    const contentcomponent = contentcomponent;
 
    /**
     * 加载状态
     */
    const [loading, setloading] = usestate(false);
 
    /**
     * 滚动容器引用
     */
    const scrollref = useref(null);
 
    let contentstyle = {height: '30px', width:'100%', textalign:'center', display:'none'};
 
    if (loading){ // 加载中显示
        contentstyle = {height: '30px', width:'100%', textalign:'center'};
        scrollref.current.scrolltop = 0; // 滚到头部
    }
 
    const handlescroll = ()=>{
        const {scrolltop} = scrollref.current;
 
        if (scrolltop > 50 && !loading){
            setloading(true); // 设置为加载中状态
 
            // 模拟数据加载
            settimeout(()=>{
                setloading(false); // 加载完成
            }, 3000)
        }
 
    }
    return (
        <div style={{height: '200px', overflow:'auto', width:'40%'}} ref={scrollref} onscroll={handlescroll}>
            <div style={contentstyle}>
                <loadingcomponent/>
            </div>
            <div style={{height:'300px', width:'100%'}}>
                <contentcomponent/>
            </div>
        </div>
    )
}

使用demo

 
import {scrollview} from "./component/scroll-view/scrollview";
 
const app = ()=> {
 
    return (
       <scrollview loadingcomponent={loading} contentcomponent={content}>
       </scrollview>
    )
}
const loading = ()=>{
    return <div>loading</div>
}
const content  = ()=>{
    return <div> hello, world</div>
}
 
 
 
export default app;

番外:上拉加载实现

1、下载react-pullload

npm i react-pullload

2、在组件中去引用

import reactpullload,{ stats } from "react-pullload";

3、css样式

①引用插件内的样式

import "node_modules/react-pullload/dist/reactpullload.css";

②或者直接引入使用下列代码:  

    .pull-load {
 
                      position: relative;
 
                      overflow-y: scroll;
 
                      -webkit-overflow-scrolling: touch;
 
            }
 
            .pull-load-head {
 
                      position: absolute;
 
                      transform: translate3d(0px, -100%, 0px);
 
                      width: 100%;
 
            }
 
            .state-refreshing .pull-load-head,
 
            .state-refreshed .pull-load-head {
 
                      position: relative;
 
                      transform: none;
 
            }
 
            .pull-load-body {
 
                      position: relative;
 
            }
 
            .state-refreshing .pull-load-body {
 
                      transition: transform 0.2s;
 
            }
 
            .state-reset .pull-load-body {
 
                      transition: transform 0.2s;
 
            }
 
            .pull-load-head-default {
 
                      text-align: center;
 
                      font-size: 12px;
 
                      line-height: 0.8rem;
 
                      color: #7676a1;
 
            }
 
            .state-pulling .pull-load-head-default:after {
 
                      content: '下拉刷新';
 
            }
 
            .state-pulling.enough .pull-load-head-default:after {
 
                      content: '松开刷新';
 
            }
 
            .state-refreshing .pull-load-head-default:after {
 
                      content: '正在刷新...';
 
            }
 
            .state-refreshed .pull-load-head-default:after {
 
                      content: '刷新成功';
 
            }
 
            .state-pulling .pull-load-head-default {
 
                      opacity: 1;
 
            }
 
            .state-pulling .pull-load-head-default i {
 
                  display: inline-block;
 
                  font-size: 0.3rem;
 
                  margin-right: .6em;
 
                  margin-top: -3px;
 
                  vertical-align: middle;
 
                  height: 0.3rem;
 
                  border-left: 1px solid;
 
                  position: relative;
 
                  transition: transform .3s ease;
 
            }
 
            .state-pulling .pull-load-head-default i:before,
 
            .state-pulling .pull-load-head-default i:after {
 
                  content: '';
 
                  position: absolute;
 
                  font-size: .5em;
 
                  width: 1em;
 
                  bottom: 0px;
 
                  border-top: 1px solid;
 
            }
 
            .state-pulling .pull-load-head-default i:before {
 
                  right: 1px;
 
                  transform: rotate(50deg);
 
                  transform-origin: right;
 
            }
 
            .state-pulling .pull-load-head-default i:after {
 
                  left: 0px;
 
                  transform: rotate(-50deg);
 
                  transform-origin: left;
 
            }
 
            .state-pulling.enough .pull-load-head-default i {
 
                  transform: rotate(180deg);
 
            }
 
            .state-refreshing .pull-load-head-default i {
 
                  margin-right: 10px;
 
                  margin-top: -3px;
 
                  display: inline-block;
 
                  vertical-align: middle;
 
                  font-size: 0.3rem;
 
                  width: 0.3rem;
 
                  height: 0.3rem;
 
                  border: 2px solid #9494b6;
 
                  border-top-color: #fff;
 
                  border-radius: 100%;
 
                  animation: circle .8s infinite linear;
 
            }
 
            .state-refreshed .pull-load-head-default {
 
                  opacity: 1;
 
                  transition: opacity 1s;
 
            }
 
            .state-refreshed .pull-load-head-default i {
 
                display: inline-block;
 
                box-sizing: content-box;
 
                vertical-align: middle;
 
                margin-right: 10px;
 
                margin-top: -3px;
 
                font-size: 14px;
 
                height: 1em;
 
                width: 1em;
 
                border: 2px solid;
 
                border-radius: 100%;
 
                position: relative;
 
         }
 
         .state-refreshed .pull-load-head-default i:before {
 
               content: '';
 
              position: absolute;
 
              top: -2px;
 
              left: 4px;
 
              height: 11px;
 
              width: 5px;
 
              border: solid;
 
              border-width: 0 2px 2px 0;
 
              transform: rotate(40deg);
 
        }
 
        .pull-load-footer-default {
 
              text-align: center;
 
              font-size: 12px;
 
              line-height: 0.8rem;
 
              color: #7676a1;
 
       }
 
        .state-loading .pull-load-footer-default:after {
 
              content: '加载更多';
 
        }
 
        .pull-load-footer-default.nomore:after {
 
              content: '没有更多';
 
        }
 
        .state-loading .pull-load-footer-default i {
 
              margin-right: 10px;
 
              margin-top: -3px;
 
              display: inline-block;
 
              vertical-align: middle;
 
              font-size: 0.3rem;
 
              width: 0.3rem;
 
              height: 0.3rem;
 
              border: 2px solid #9494b6;
 
              border-top-color: #fff;
 
              border-radius: 100%;
 
              animation: circle .8s infinite linear;
 
        }
 
        @keyframes circle {
 
             100% {
 
                    transform: rotate(360deg);
 
              }
 
        }

4、pullload标签包裹

<reactpullload
 
                    downenough={150}
 
                    ref="reactpullload"
 
                    classname="block"
 
                     *加上下面注释的属性会出问题
 
                    // isblockcontainer={true}
 
                    action={this.state.action}
 
                    handleaction={this.handleaction}
 
                    hasmore={this.state.hasmore}
 
                    style={{paddingtop: 132}}
 
                    distancebottom={1000}>
 
                    .....  this is list
 
                    </reactpullload>

5、scroll函数

constructor(props) {
 
        super(props);
 
        this.state = {
 
              // scroll 相关
 
              hasmore: true,
 
              action: stats.init,
 
        }
 
    }
 
// 滚动条
 
    handleaction = (action) => {
 
      if(action === this.state.action ||
 
        action === stats.refreshing && this.state.action === stats.loading ||
 
        action === stats.loading && this.state.action === stats.refreshing){
 
        return false
 
      }
 
      if(action === stats.refreshing){//刷新
 
          settimeout(()=>{
 
                  //refreshing complete
 
                下拉刷新
 
          }, 1000)
 
      } else if(action === stats.loading){//加载更多
 
        this.setstate({
 
          hasmore: true
 
        });
 
        settimeout(()=>{
 
                  if(this.state.index === this.state.curpage){
 
                         this.setstate({
 
                                 action: stats.reset,
 
                                  hasmore: false
 
                           });
 
                        } else{
 
                                    上拉加载....
 
                          }
 
            }, 1000)
 
      }
 
      this.setstate({
 
        action: action
 
      })
 
    }

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com