H5页面上拉加载更多功能实现

首先定义获取页面当前的滚动高度方法:

复制代码
  //滚动条在Y轴上的滚动距离
        getScrollTop() {
            var documentScrollTop = 0;
            documentScrollTop = document.documentElement.scrollTop;
            return documentScrollTop;
        },
        //文档的总高度
        getScrollHeight() {
            var documentScrollHeight = 0;
            documentScrollHeight = document.documentElement.scrollHeight;
            return documentScrollHeight;
        },
        //浏览器视口的高度
        getWindowHeight() {
            var windowHeight = 0;
            windowHeight = document.documentElement.clientHeight;
            return windowHeight;
        },
复制代码

其次对当前页面的滚动高度进行计算:

复制代码
 // 锚点定位
        initHeight() {
            // 设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 (被卷曲的高度)
            var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
            if(scrollTop > (this.$refs.product_detail_recommend.offsetTop - 60) && this.recommend.length > 0){
                this.tabIndex = 3;
            }else 
            if(scrollTop > (this.$refs.product_detail_html.offsetTop - 60)){
                this.tabIndex = 2;
            }else if(scrollTop > (this.$refs.product_detail_eval.offsetTop - 60)){
                this.tabIndex = 1;
            }else{
                this.tabIndex = 0
            }
            // 头部导航背景色渐变
            if (scrollTop > 80) {
                let opacity = scrollTop / 140
                opacity = opacity > 1 ? 1 : opacity
                this.showFixedOpacity = { opacity }
                this.showFixed = false
            } else {
                this.showFixed = true
            }
            //如果被卷曲的高度大于吸顶元素到顶端位置 的距离
            // this.isFixed = scrollTop > this.offsetTop ? true : false;
            if(this.isScroll){
                let scroll =
                this.getScrollTop() + this.getWindowHeight() - this.getScrollHeight();
                if (scroll > -1) {    //距离底部多少像素开始触发
                    this.loadMoreEvaData();
                }
            }
        },
复制代码

定义滚动加载的方法:

复制代码
//下拉加载
        loadMoreEvaData(){
            this.pageNum ++ ;
            if(this.pageNum >= this.totalPage ){
                this.isScroll = false
            }else{
                this.getRecommendData();
            }
        },
复制代码

数据请求的方法定义:

复制代码
  // 推荐列表
        getRecommendData(){
            this.$http.get(urlList.getRecommends,{params:{
                productId: this.productId,
                locationType: 0,
                pageNum: this.pageNum
            }}).then(res=>{
                if(res.data.code == 200){
                    var res = res.data.data;
                    this.totalPage = res.totalPage;
                    if(this.totalPage == 0){
                        this.isScroll = false
                    }
                    this.recommend = this.recommend.concat(res.recommend);
                }
            })
        },
复制代码

在生命周期里调用滚动监听并对滚动监听进行解绑:

复制代码
   mounted() {
        this.getRecommendData();
        window.addEventListener('scroll', this.initHeight);
    },
    //离开删除绑定事件
    beforeDestroy() {
        document.body.style.overflow = 'scroll'
        document.removeEventListener('touchmove', preD, {passive: false})
        window.removeEventListener("scroll",this.initHeight)
    },
复制代码

写在最后不要忘记在data里面定义 允许触底条件,否则滚动监听无效

isScroll:true, //是否允许触发触底事件
posted @   巫小婆  阅读(1371)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示