react移动端上拉加载更多组件
在开发移动端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移动端上拉加载更多组件的更多相关文章
- react-native 自定义 下拉刷新 / 上拉加载更多 组件
1.封装 Scroller 组件 /** * 下拉刷新/上拉加载更多 组件(Scroller) */ import React, {Component} from 'react'; import { ...
- vue移动端上拉加载更多
LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...
- 微信小程序 - 上拉加载更多组件
详情用例看demo,点击下载示例:loadmore
- react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...
- 原生js移动端touch事件实现上拉加载更多
大家都知道jQuery里没有touch事件,所以在移动端使用原生js实现上拉加载效果还是很不错的,闲话不多说,代码如下: //获取要操作的元素 var objSection = document.ge ...
- 移动端touch事件 || 上拉加载更多
前言: 说多了都是泪,在进行项目开发时,在上拉加载更多实现分页效果的问题上,由于当时开发任务紧急,所以就百度找了各种移动端的上拉下拉 实现加载更多的插件.然后就留下了个坑:上拉加载的时候会由于用户错误 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- 【Web】移动端下拉刷新、上拉加载更多插件
移动网站中常常有的功能:列表的下拉刷新.上拉加载更多 本例介绍一种简单使用的移动端下拉刷新.上拉加载更多插件,下载及参考地址:https://github.com/ximan/dropload 插件依 ...
- 移动端实现上拉加载更多(使用dropload.js vs js)
做下笔记,:移动端实现上拉加载更多,其实是数据的分段加载,在这里为了做测试我写了几个json文件作为分段数据: 方式一:使用dropload.js; 配置好相关参数及回调函数就可使用:代码如下 var ...
随机推荐
- ics-05
挺有意思的一题 攻防世界->web->ics-05 打开题目链接,就是一个很正常的管理系统,只有左侧的可以点着玩 并且点到**设备维护中心时,页面变为index.php 查看响应 发现云平 ...
- bugku_MagicImageViewer
CTF 安卓逆向 MagicImageViewer--png结构+算法 很少做安卓逆向的题目,在此记录一下 先用模拟器看一下 嗯,没啥提示. jeb打开 关键部分 if(s.length() == 1 ...
- C++面试八股文:std::vector和std::list,如何选择?
某日二师兄参加XXX科技公司的C++工程师开发岗位第24面: 面试官:list用过吗? 二师兄:嗯,用过. 面试官:请讲一下list的实现原理. 二师兄:std::list被称为双向链表,和C中手写双 ...
- React后台管理系统11 配置项目初始化展开代码
在上一文中,我们已经配置好了,刷新默认打开选中的样式,但是如果是在/page3/1,这种的,并没有选中到/page3里面的/page3/1,这个地方来,所以我们需要解决的就是这几个问题: 思路如下: ...
- Java 统计大串中小串出现的次数 举例:在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"中java出现了5次
代码如下: public static void main(String[] args) { //大串 String max = "woaijavawozhenaijavawozhendea ...
- 逍遥自在学C语言 | 函数初级到高级解析
前言 函数是C语言中的基本构建块之一,它允许我们将代码组织成可重用.模块化的单元. 本文将逐步介绍C语言函数的基础概念.参数传递.返回值.递归以及内联函数和匿名函数. 一.人物简介 第一位闪亮登场,有 ...
- 关于SQL SERVER 字段类型char(n) , nchar(n) , varchar(n) , nvarchar(n)
对于很多新手来说,经常被字段类型搞得晕头转向,今天我用通俗易懂的解释帮大家理解这些类型. 在数据库字段类型定义中,可以分为两大类,一类为Unicode类型,另一种就是非Unicode. Unicode ...
- RSA 加密签名验签解密
import javax.crypto.Cipher; import javax.crypto.spec.OAEPParameterSpec; import javax.crypto.spec.PSo ...
- Spring原理之web.xml加载过程
web.xml是部署描述文件,它不是Spring所特有的,而是在Servlet规范中定义的,是web应用的配置文件.web.xml主要是用来配置欢迎页.servlet.filter.listener等 ...
- Jenkins快速入门部署+实践
安装 方法一 Jenkins中文网下载jenkins.war 方法二 直接从http://mirrors.jenkins-ci.org/war/latest/jenkins.war下载最新的war包, ...