在开发移动端react项目中,遇到了上拉加载更多数据的分页功能,自己封装了一个组件,供大家参考,写的不好还请多多指教!
  

  1. import React, {Component} from 'react';
  2. import cssModuleHandler from "../../../utils/cssModuleHandler";
  3. import styleObject from './LoadMore.scss';
  4.  
  5. const GSV = cssModuleHandler(styleObject);
  6.  
  7. class LoadMore extends Component {
  8. constructor(props) {
  9. super(props);
  10. }
  11.  
  12. componentDidMount() {
  13. const loadMoreFn = this.props.loadMoreFn;
  14.  
  15. const callback = () => {
  16. if (this.getScrollTop() + this.getWindowHeight() + 100 > this.getScrollHeight()) {
  17. loadMoreFn();
  18. }
  19. }
  20.  
  21. //滚动事件
  22. let timeAction;
  23. window.addEventListener('scroll', () => {
  24. if (this.props.isLoadingMore) {
  25. return;
  26. }
  27.  
  28. if (timeAction) {
  29. clearTimeout(timeAction);
  30. }
  31.  
  32. timeAction = setTimeout(callback, 50);
  33. });
  34. }
  35.  
  36. //滚动条在Y轴上的滚动距离
  37. getScrollTop() {
  38. let scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
  39. if (document.body) {
  40. bodyScrollTop = document.body.scrollTop;
  41. }
  42. if (document.documentElement) {
  43. documentScrollTop = document.documentElement.scrollTop;
  44. }
  45. scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
  46. return scrollTop;
  47. }
  48.  
  49. //文档的总高度
  50. getScrollHeight() {
  51. let scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
  52. if (document.body) {
  53. bodyScrollHeight = document.body.scrollHeight;
  54. }
  55. if (document.documentElement) {
  56. documentScrollHeight = document.documentElement.scrollHeight;
  57. }
  58. scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
  59. return scrollHeight;
  60. }
  61.  
  62. //浏览器视口的高度
  63. getWindowHeight() {
  64. let windowHeight = 0;
  65. if (document.compatMode == "CSS1Compat") {
  66. windowHeight = document.documentElement.clientHeight;
  67. } else {
  68. windowHeight = document.body.clientHeight;
  69. }
  70. return windowHeight;
  71. }
  72.  
  73. render() {
  74. return (
  75. <React.Fragment>
  76. {this.props.isLoadingMore
  77. ? <div className={GSV('loadMore')} ref='wrapper'><img
  78. src={require('../../../static/img/common/ic/spinner.png')} alt=""/></div>
  79. : ''}
  80. </React.Fragment>
  81. )
  82. }
  83. }
  84.  
  85. export default LoadMore;

 在需要分页的组件中引入LoadMore组件:

  1. import React, {Component} from 'reactimport {observer, inject} from "mobx-react";import {InputItem} from 'antd-mobile';
  2.  
  3. import LoadMore from "../../../../../components/Commoncomponent/LoadMore/LoadMore";
  4. import PrizeList from "./view/PrizeList/PrizeList";
  5. import cssModuleHandler from "../../../utils/cssModuleHandler";
  6. import styleObject from './index.scss';
  7.  
  8. const GSV = cssModuleHandler(styleObject);
  9.  
  10. @inject("commonAction", "commonStore", "giftCouponStore", "giftCouponAction")
  11. @observer
  12. class GiftCoupon extends Component {
  13. constructor(props) {
  14. super(props);
  15.  
  16. this.pageNo = 0;
  17. this.itemPerPage = 200;
  18. }
  19.  
  20. componentDidMount() {
  21. //初始化数据
  22. this.loadMoreFn();
  23. }
  24.  
  25. //加载更多
  26. loadMoreFn = () => {
  27. this.pageNo++;
  28.  
  29. this.props.giftCouponAction.queryBonusListPage({
  30. pageNo: this.pageNo,
  31. itemPerPage: this.itemPerPage
  32. });
  33. }
  34.  
  35. render() {
  36. const {giftCouponList, isLoadingMore} = this.props.giftCouponStore;
  37.  
  38. return (
  39. <div className={GSV('otherGiftCouponWrapper')}>
  40. {
  41. giftCouponList.length > 0 &&
  42. {/*礼券列表*/}
  43. <PrizeList giftCouponAction={this.props.giftCouponAction}
  44. data={giftCouponList}
  45. loadMoreFn={this.loadMoreFn}
  46. isLoadingMore={isLoadingMore}
  47. />
  48.  
  49. }
  50.           {/*加载更多*/}
  51.           <LoadMore loadMoreFn={loadMoreFn} isLoadingMore={isLoadingMore}/>
  52.  
  53. </div>
  54. )
  55. }
  56. }
  57.  
  58. export default GiftCoupon;

  

react移动端上拉加载更多组件的更多相关文章

  1. react-native 自定义 下拉刷新 / 上拉加载更多 组件

    1.封装 Scroller 组件 /** * 下拉刷新/上拉加载更多 组件(Scroller) */ import React, {Component} from 'react'; import { ...

  2. vue移动端上拉加载更多

    LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...

  3. 微信小程序 - 上拉加载更多组件

    详情用例看demo,点击下载示例:loadmore

  4. react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)

    1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...

  5. 原生js移动端touch事件实现上拉加载更多

    大家都知道jQuery里没有touch事件,所以在移动端使用原生js实现上拉加载效果还是很不错的,闲话不多说,代码如下: //获取要操作的元素 var objSection = document.ge ...

  6. 移动端touch事件 || 上拉加载更多

    前言: 说多了都是泪,在进行项目开发时,在上拉加载更多实现分页效果的问题上,由于当时开发任务紧急,所以就百度找了各种移动端的上拉下拉 实现加载更多的插件.然后就留下了个坑:上拉加载的时候会由于用户错误 ...

  7. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...

  8. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...

  9. 【Web】移动端下拉刷新、上拉加载更多插件

    移动网站中常常有的功能:列表的下拉刷新.上拉加载更多 本例介绍一种简单使用的移动端下拉刷新.上拉加载更多插件,下载及参考地址:https://github.com/ximan/dropload 插件依 ...

  10. 移动端实现上拉加载更多(使用dropload.js vs js)

    做下笔记,:移动端实现上拉加载更多,其实是数据的分段加载,在这里为了做测试我写了几个json文件作为分段数据: 方式一:使用dropload.js; 配置好相关参数及回调函数就可使用:代码如下 var ...

随机推荐

  1. ics-05

    挺有意思的一题 攻防世界->web->ics-05 打开题目链接,就是一个很正常的管理系统,只有左侧的可以点着玩 并且点到**设备维护中心时,页面变为index.php 查看响应 发现云平 ...

  2. bugku_MagicImageViewer

    CTF 安卓逆向 MagicImageViewer--png结构+算法 很少做安卓逆向的题目,在此记录一下 先用模拟器看一下 嗯,没啥提示. jeb打开 关键部分 if(s.length() == 1 ...

  3. C++面试八股文:std::vector和std::list,如何选择?

    某日二师兄参加XXX科技公司的C++工程师开发岗位第24面: 面试官:list用过吗? 二师兄:嗯,用过. 面试官:请讲一下list的实现原理. 二师兄:std::list被称为双向链表,和C中手写双 ...

  4. React后台管理系统11 配置项目初始化展开代码

    在上一文中,我们已经配置好了,刷新默认打开选中的样式,但是如果是在/page3/1,这种的,并没有选中到/page3里面的/page3/1,这个地方来,所以我们需要解决的就是这几个问题: 思路如下: ...

  5. Java 统计大串中小串出现的次数 举例:在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"中java出现了5次

    代码如下: public static void main(String[] args) { //大串 String max = "woaijavawozhenaijavawozhendea ...

  6. 逍遥自在学C语言 | 函数初级到高级解析

    前言 函数是C语言中的基本构建块之一,它允许我们将代码组织成可重用.模块化的单元. 本文将逐步介绍C语言函数的基础概念.参数传递.返回值.递归以及内联函数和匿名函数. 一.人物简介 第一位闪亮登场,有 ...

  7. 关于SQL SERVER 字段类型char(n) , nchar(n) , varchar(n) , nvarchar(n)

    对于很多新手来说,经常被字段类型搞得晕头转向,今天我用通俗易懂的解释帮大家理解这些类型. 在数据库字段类型定义中,可以分为两大类,一类为Unicode类型,另一种就是非Unicode. Unicode ...

  8. RSA 加密签名验签解密

    import javax.crypto.Cipher; import javax.crypto.spec.OAEPParameterSpec; import javax.crypto.spec.PSo ...

  9. Spring原理之web.xml加载过程

    web.xml是部署描述文件,它不是Spring所特有的,而是在Servlet规范中定义的,是web应用的配置文件.web.xml主要是用来配置欢迎页.servlet.filter.listener等 ...

  10. Jenkins快速入门部署+实践

    安装 方法一 Jenkins中文网下载jenkins.war 方法二 直接从http://mirrors.jenkins-ci.org/war/latest/jenkins.war下载最新的war包, ...