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 ...
随机推荐
- .NET周报 【5月第3期 2023-05-21】
国内文章 C# 实现 Linux 视频会议(源码,支持信创环境,银河麒麟,统信UOS) https://www.cnblogs.com/shawshank/p/17390248.html 信创是现阶段 ...
- IT入门深似海,入门到放弃你学废了嘛
我一直觉得IT行业 程序员行业.甚至觉得程序员人群 是一个特殊存在的群体. 入门到放弃,是真的,IT门槛高嘛. 其实吧,IT编程门槛,是有的,但是对于感兴趣的,想学习IT编程同学来说,也是一件容易事情 ...
- 【技术积累】Java中的JVM【一】
什么是JVM JVM英文全称为Java Virtual Machine,中文意为Java虚拟机.JVM是一种能够执行Java语言编写的程序的虚拟机器,它首次作为Java语言的一部分,后来又被移植到了许 ...
- 【LGR-142-Div.4】洛谷入门赛 #13 赛后总结
A.魔方 目测入门 -,就是需要开long long //1 #include<bits/stdc++.h> typedef long long valueType; int main() ...
- 云原生时代崛起的编程语言Go远程调用gRPC实战
@ 目录 概述 定义 背景 特点 四种服务方法 实战 环境配置 proto文件 简单RPC Token认证 服务器流式RPC 客户端流式RPC 双向流式RPC 概述 定义 gRPC 官网地址 http ...
- 前端学习C语言 - 数组和字节序
数组 本篇主要介绍:一维二维数组.字符数组.数组名和初始化注意点以及字节序. 一维数组 初始化 有以下几种方式对数组初始化: // 定义一个有5个元素的数组,未初始化 int a[5]; // 定义一 ...
- 用写代码的方式画图-试下PlantUML吧
1 序言 所谓一图胜千言,大家平日在工作中编写文档时,往往都需要画各种图来表达中心思想,比如流程图.时序图.UML 图,很多人选择使用 Axure .PrecessOn.Diagrams(darw.i ...
- Python 学习路线图
Python 学习路线图 在这个系列笔记中,我将陆续整理自己在学习 Python 编程语言及其框架的过程中留下的笔记和代码,目的是掌握如何在生产环境中利用各种领域的第三方框架来快速开发应用程序.和大多 ...
- 【Jenkins】 GitLab Gitee GitHub 部署
Jenkins GitLab Gitee GitHub 部署 环境 Jenkins Git Maven Jenkins 部署可参考文章:https://www.cnblogs.com/cxt618/p ...
- typedef和define有什么区别
typedef 和define 都是替一个对象取一个别名,以此增强程序的可读性,区别如下: 使用不用 define 定义后面不用加分号,并且它的别名在对象的前面 typedef需要加分号,并且它的别名 ...