最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于React實現(xiàn)下拉刷新效果

 更新時間:2024年03月22日 09:43:56   作者:卡卡舅舅  
這篇文章主要介紹了如何基于react實現(xiàn)下拉刷新效果,在下拉的時候會進(jìn)入loading狀態(tài),文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

簡介

本文基于react實現(xiàn)下拉刷新效果,在下拉的時候會進(jìn)入loading狀態(tài)。

實現(xiàn)效果

效果如上圖所示,在下拉到底部時候,會出現(xiàn)loading條,在處理完成后loading條消失。

具體代碼

布局 & 邏輯

import {useRef, useState} from "react";
 
export const ScrollView = ({loadingComponent, contentComponent}) => {
    const LoadingComponent = loadingComponent;
    const ContentComponent = contentComponent;
 
    /**
     * 加載狀態(tài)
     */
    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); // 設(shè)置為加載中狀態(tài)
 
            // 模擬數(shù)據(jù)加載
            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;

番外:上拉加載實現(xiàn)

1、下載react-pullload

npm i react-pullload

2、在組件中去引用

import ReactPullLoad,{ STATS } from "react-pullload";

3、css樣式

①引用插件內(nèi)的樣式

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標(biāo)簽包裹

<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函數(shù)

constructor(props) {
 
        super(props);
 
        this.state = {
 
              // scroll 相關(guān)
 
              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
 
      })
 
    }

到此這篇關(guān)于基于React實現(xiàn)下拉刷新效果的文章就介紹到這了,更多相關(guān)React下拉刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React+TypeScript+webpack4多入口配置詳解

    React+TypeScript+webpack4多入口配置詳解

    這篇文章主要介紹了React+TypeScript+webpack4多入口配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • React中綁定this并給函數(shù)傳參的三種方式

    React中綁定this并給函數(shù)傳參的三種方式

    React中定義一個組件,可以通過ES6版本以前的React.createClass或者ES6的class xxx extends React.Component,綁定this是react中非常重要的一部分,React中,綁定this的方式有多種,下面一一看看怎么進(jìn)行this綁定并給函數(shù)傳參,需要的朋友可以參考下
    2025-07-07
  • React事件處理的機(jī)制及原理

    React事件處理的機(jī)制及原理

    這篇文章主要介紹了React事件處理的機(jī)制及原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • React實現(xiàn)組件間通信的幾種方式小結(jié)

    React實現(xiàn)組件間通信的幾種方式小結(jié)

    在React應(yīng)用中,組件間的通信是一個基礎(chǔ)而關(guān)鍵的概念,理解和掌握不同組件之間的通信方式,可以幫助我們構(gòu)建出更加模塊化、可維護(hù)和可擴(kuò)展的應(yīng)用程序,React提供了多種組件通信的方法,本文給大家詳細(xì)的介紹了這些方法,需要的朋友可以參考下
    2024-07-07
  • React處理表單輸入的幾種主要方法

    React處理表單輸入的幾種主要方法

    在現(xiàn)代前端開發(fā)中,表單是用戶與應(yīng)用程序進(jìn)行交互的最主要方式之一,React,作為一款強(qiáng)大的視圖庫,提供了多種靈活的方式來處理表單數(shù)據(jù),本文將深入探討 React 中處理表單輸入的幾種主要方法,需要的朋友可以參考下
    2025-08-08
  • React Scheduler 最小堆實現(xiàn)小結(jié)

    React Scheduler 最小堆實現(xiàn)小結(jié)

    本文主要介紹了React Scheduler 最小堆實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • react-redux多個組件數(shù)據(jù)共享的方法

    react-redux多個組件數(shù)據(jù)共享的方法

    這篇文章主要介紹了react-redux多個組件數(shù)據(jù)共享的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • react實現(xiàn)導(dǎo)航欄二級聯(lián)動

    react實現(xiàn)導(dǎo)航欄二級聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了react實現(xiàn)導(dǎo)航欄二級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • React組件通信實現(xiàn)方式詳解

    React組件通信實現(xiàn)方式詳解

    這篇文章主要介紹了React組件通信,在開發(fā)中組件通信是React中的一個重要的知識點,本文通過實例代碼給大家講解react中常用的父子、跨組件通信的方法,需要的朋友可以參考下
    2023-03-03
  • React中setState更新狀態(tài)的兩種寫法

    React中setState更新狀態(tài)的兩種寫法

    在?React?中,我們經(jīng)常需要更新組件的狀態(tài)(state),使用?setState?方法是一種常見的方式來實現(xiàn)狀態(tài)的更新,而在使用?setState?方法時,有兩種不同的寫法,即對象式和函數(shù)式,本文將介紹這兩種寫法的區(qū)別和使用場景,需要的朋友可以參考下
    2024-03-03

最新評論

临桂县| 丰顺县| 聂拉木县| 永川市| 根河市| 西昌市| 布尔津县| 侯马市| 南溪县| 九龙坡区| 凤山县| 驻马店市| 芦山县| 蕲春县| 太康县| 宜兰县| 武山县| 玉林市| 临泉县| 中方县| 靖远县| 哈密市| 京山县| 卢湾区| 页游| 罗城| 桃源县| 建瓯市| 涟源市| 尼勒克县| 英山县| 博爱县| 吉林市| 称多县| 漠河县| 西丰县| 松原市| 丹寨县| 呼图壁县| 确山县| 仁寿县|