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

import React, {Component} from 'react';
import cssModuleHandler from "../../../utils/cssModuleHandler";
import styleObject from './LoadMore.scss'; const GSV = cssModuleHandler(styleObject); class LoadMore extends Component {
constructor(props) {
super(props);
} componentDidMount() {
const loadMoreFn = this.props.loadMoreFn; const callback = () => {
if (this.getScrollTop() + this.getWindowHeight() + 100 > this.getScrollHeight()) {
loadMoreFn();
}
} //滚动事件
let timeAction;
window.addEventListener('scroll', () => {
if (this.props.isLoadingMore) {
return;
} if (timeAction) {
clearTimeout(timeAction);
} timeAction = setTimeout(callback, 50);
});
} //滚动条在Y轴上的滚动距离
getScrollTop() {
let scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
} //文档的总高度
getScrollHeight() {
let scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
} //浏览器视口的高度
getWindowHeight() {
let windowHeight = 0;
if (document.compatMode == "CSS1Compat") {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
} render() {
return (
<React.Fragment>
{this.props.isLoadingMore
? <div className={GSV('loadMore')} ref='wrapper'><img
src={require('../../../static/img/common/ic/spinner.png')} alt=""/></div>
: ''}
</React.Fragment>
)
}
} export default LoadMore;

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

import React, {Component} from 'reactimport {observer, inject} from "mobx-react";import {InputItem} from 'antd-mobile';

import LoadMore from "../../../../../components/Commoncomponent/LoadMore/LoadMore";
import PrizeList from "./view/PrizeList/PrizeList";
import cssModuleHandler from "../../../utils/cssModuleHandler";
import styleObject from './index.scss'; const GSV = cssModuleHandler(styleObject); @inject("commonAction", "commonStore", "giftCouponStore", "giftCouponAction")
@observer
class GiftCoupon extends Component {
constructor(props) {
super(props); this.pageNo = 0;
this.itemPerPage = 200;
} componentDidMount() {
//初始化数据
this.loadMoreFn();
} //加载更多
loadMoreFn = () => {
this.pageNo++; this.props.giftCouponAction.queryBonusListPage({
pageNo: this.pageNo,
itemPerPage: this.itemPerPage
});
} render() {
const {giftCouponList, isLoadingMore} = this.props.giftCouponStore; return (
<div className={GSV('otherGiftCouponWrapper')}>
{
giftCouponList.length > 0 &&
{/*礼券列表*/}
<PrizeList giftCouponAction={this.props.giftCouponAction}
data={giftCouponList}
loadMoreFn={this.loadMoreFn}
isLoadingMore={isLoadingMore}
/> }
          {/*加载更多*/}
          <LoadMore loadMoreFn={loadMoreFn} isLoadingMore={isLoadingMore}/> </div>
)
}
} 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. Uncaught TypeError: imageStyle.getImageState is not a function

    这个错误也是遇得到哟,柑橘自己好无辜呀,我哪里错了,找了半天原来还是自己找的错误 看 import Circle from 'ol/geom/Circle'; feature.setStyle(new ...

  2. Python-PyQt5的安装与简单使用

    一.安装 1.安装 PyQt5 和 PyQt5-tools pip install PyQt5 -i https://pypi.douban.com/simple pip install PyQt5- ...

  3. SPSS统计教程:卡方检验

    本文简要的介绍了卡方分布.卡方概率密度函数和卡方检验,并通过SPSS实现了一个卡方检验例子,不仅对结果进行了解释,而且还给出了卡方.自由度和渐近显著性的计算过程.本文用到的数据"2.2.sa ...

  4. 4. DI相关内容

    我们先来思考 向一个类中传递数据的方式有几种? 普通方法(set 方法) 构造方法 依赖注入描述了在容器中建立 bean 与 bean 之间的依赖关系的过程,如果 bean 运行需要的是数字或字符串呢 ...

  5. 论文翻译:2021_Real-Time Denoising and Dereverberation wtih Tiny Recurrent U-Net

    论文地址:微型循环U-Net实时降噪和去混响 论文代码: https://github.com/YangangCao/TRUNet https://github.com/amirpashamobini ...

  6. Windows服务启动exe无界面终极解决方案

      1.前言 我这个方案(C#操作)是彻底解决[从Windows服务启动程序exe,程序无界面]问题的终极解决方案,终极方案,绝对的终极方案,本来打算收钱的,还是算了,你们也不容易,关注我一下就行.后 ...

  7. 深入解析Redis的LRU与LFU算法实现

    作者:vivo 互联网服务器团队 - Luo Jianxin 重点介绍了Redis的LRU与LFU算法实现,并分析总结了两种算法的实现效果以及存在的问题. 一.前言 Redis是一款基于内存的高性能N ...

  8. 行行AI人才直播第7期:奇计AI创始人左晟《AI时代的商业挑战和机遇》

    行行AI人才是博客园和顺顺智慧共同运营的AI行业人才全生命周期服务平台,是园子商业化努力的一个重要方向. 行行AI人才直播希望以直播的方式让大家更多了解AI行业的现状与未来可能的发展方向. 随着人工智 ...

  9. 【Mybatis】动态SQL

    目录 动态SQL if语句 动态SQL if+where语句 动态SQL if+set语句 动态SQL choose(when,otherwise)语句 动态SQL trim语句 动态SQL SQL片 ...

  10. Hexo博客Next主题添加粒子时钟特效

    博客应用canvas粒子时钟的操作步骤: 在\themes\next\layout\_custom\目录下,新建clock.swig文件,内容如下: <div style="" ...