移动端滚动加载方法,及防止重复加载

在移动端滚动加载时,因为最开始用的Window.onscroll导致在不同手机滚动效果不同(分辨率大的滚动加载没有效果,分辨率小的滚动重复加载)

mounted() {
    window.addEventListener('scroll', this.lazyLoading); // 滚动到底部,再加载的处理事件
}
beforeDestroy () {
    window.removeEventListener('scroll', this.lazyLoading);    //离开页面时移除
  },
methods: {
	const self = this
     const scrollTop = $(window).scrollTop();
     const scrollHeight = $(document).height();
     const windowHeight = $(window).height();
     this.scrollHeight=scrollHeight;
     if(scrollHeight-(scrollTop + windowHeight)<80) {
     //判断数据
       if (self.articles.length >= 1) {
         self.busy = false;
         //pageBool防止重复请求,在loadArticlesItem功能中设置为true
         if(self.pageBool){
           self.pageBool=false
           //请求事件
           self.loadArticlesItem(1);
         }
       } else {
       }
     }
   },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在React中实现移动滑动可以使用一些第三方库来帮助我们完成。 其中比较常用的是react-infinite-scroll-component。该库实现了无限滚动的功能,当用户滚动到页面底部时会触发一个回调函数来更多数据,从而完成滑动的效果。 具体使用方法如下: 1. 安装库 ``` npm install react-infinite-scroll-component --save ``` 2. 引入库 ```javascript import InfiniteScroll from 'react-infinite-scroll-component'; ``` 3. 实现滑动的组件 ```javascript import React, { Component } from 'react'; import InfiniteScroll from 'react-infinite-scroll-component'; class ScrollLoad extends Component { state = { items: [], hasMore: true, page: 1, }; componentDidMount() { this.fetchData(); } fetchData = () => { const { page } = this.state; fetch(`https://jsonplaceholder.typicode.com/photos?_page=${page}&_limit=10`) .then((response) => response.json()) .then((data) => { this.setState({ items: [...this.state.items, ...data], page: page + 1, hasMore: data.length === 10, }); }); }; render() { const { items, hasMore } = this.state; return ( <InfiniteScroll dataLength={items.length} next={this.fetchData} hasMore={hasMore} loader={<h4>Loading...</h4>} > {items.map((item) => ( <div key={item.id}> <h3>{item.title}</h3> <img src={item.url} alt={item.title} /> </div> ))} </InfiniteScroll> ); } } export default ScrollLoad; ``` 在上面的代码中,我们使用了一个列表来展示数据,同时使用了react-infinite-scroll-component来实现滑动的效果。在组件中,我们使用state来保存数据和一些状态,通过fetchData方法更多数据,当用户滑动到页面底部时,会自动触发next方法更多数据。同时,我们还设置了一个loader来显示正在中的提示。 最后,我们将列表包裹在InfiniteScroll组件中,设置hasMore属性为true,表示还有更多的数据需要,dataLength属性表示当前已经的数据的长度,next属性表示下一次需要数据的方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值