Html下拉滚动事件

一、需求

Html网页,下拉事件,下拉到底就加载数据,不断下拉不断出数据。

二、实现关键代码介绍

1.onscroll 事件在元素滚动条在滚动时触发。
https://www.runoob.com/jsref/event-onscroll.html

2.滚动条滚动时,距离顶部的距离
document.documentElement.scrollTop || document.body.scrollTop;
在这里插入图片描述

3可视区的高度
document.documentElement.clientHeight || document.body.clientHeight;
在这里插入图片描述

  1. 页面的总高度
    document.documentElement.scrollHeight || document.body.scrollHeight;
    在这里插入图片描述

三、实训案例(以Vue做案例)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <div v-html="html"></div>
</div>
<script>
    var vm = new Vue({
        el: "#app",//挂载在app1上
        data: {
            html:"<div>\n" +
                "        <div style=\"background-color: cyan;height: 35vh\"></div>\n" +
                "        <div style=\"background-color: red;height: 35vh\"></div>\n" +
                "        <div style=\"background-color: chartreuse;height: 35vh\"></div>\n" +
                "    </div>",
            //是否加载
            isExecute:true
        },
        methods: {
            //获取分享页面课程详情
            getData: function () {
                let that = this;
                that.html += that.html;
                //加载完了,放行下拉事件
                that.isExecute = true
                console.log(that.isExecute)
            },

            scroll() {
                let that = this;
                let beforeNum = 0;
                window.onscroll = function(){
                    // scrollTop 滚动条滚动时,距离顶部的距离
                    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                    // windowHeight 可视区的高度
                    var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
                    // scrollHeight 页面的总高度
                    var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
                    // 判断是否在下拉
                    if(scrollTop + windowHeight>beforeNum){
                        beforeNum = scrollTop + windowHeight;
                        // 滚动条到底部的条件
                        if(scrollTop + windowHeight > scrollHeight-20){
                            // console.log("加载数据")
                            // 是否加载数据避免方法重复执行
                            if(that.isExecute){
                                console.log("加载数据")
                                that.isExecute = false;
                                that.getData()
                            }
                        }
                    }else{
                        beforeNum = scrollTop + windowHeight;
                    }

                }

            },

        },
        mounted() {
            this.scroll()
        }
    })
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memory沙漏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值