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 ...
随机推荐
- Python基础 - 逻辑运算符
Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20: 运算符 逻辑表达式 描述 实例 and x and y 布尔"与" - 如果 x 为 False,x a ...
- Isito 入门:为什么学 Istio、Istio 是什么
1,Istio 概述 聊聊微服务设计 似乎用上 Kubernetes ,就是微服务系统了. 碰到很多人或公司盲目崇拜 Kubernetes ,一直喊着要上 Kubernetes,但是本身既没有技术储备 ...
- 【python基础】复杂数据类型-列表类型(增删改查)
1.初识列表(list) 列表由一系列按特定顺序排列的数据元素组成.可以将任何类型数据元素加入列表中,其中的数据元素之间没有任何关系.鉴于列表通常包含多个数据元素,给列表指定一个表示复数的名称是个不错 ...
- R 语言中 X11 相关的一些问题
参考 Anaconda 官方文档<Using R language with Anaconda>安装 R-4.0.2: conda create -n r-4.0.2 r-essentia ...
- 实用的windows快捷键
Alt+F4 关闭窗口 win+D 显示桌面 win+Tab 切换窗口 Alt+Tab 应用之间的切换 win+E 打开我的电脑 Ctrl+Shift+Esc 打开任务管理器 Home 回到行首 En ...
- Vue_Django 登录注册+图书管理系统
Vue前端 注册页面 点击查看代码 <template> <div class="register"> <el-row :gutter="2 ...
- XMLConfiguration -- Poco
Library : Util Package: Configuration Header : Poco/Util.XMLConfiguration.h 此配置类从 XML 文档中提取配置属性. 支持类 ...
- MyBatis体系笔记
MyBatis 什么是MyBatis MyBatis是优秀的持久层框架 MyBatis使用XML将SQL与程序解耦,便于维护 MyBatis学习简单,执行高效,是JDBC的延伸 1.MyBatis开发 ...
- React后台管理系统08 左侧菜单栏点击事件以及设置只有一个菜单展开项
我们在Menu组件身上添加一个点击事件:对应的函数写一个回调函数:获取当前对象的e的身上的key, 这里其实不难看出e就是当前点击时的menu对象,我们这里获取的是e的key,对应上面定义的属性. 此 ...
- 深度解析SpringBoot内嵌Web容器
你好,我是刘牌! 前言 今天分享一个SpringBoot的内嵌Web容器,在SpringBoot还没有出现时,我们使用Java开发了Web项目,需要将其部署到Tomcat下面,需要配置很多xml文件, ...