js判断页面到达底部

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
    *{
        margin: 0;
        padding: 0;
    }
    .a{
        height: 100vh;
        background-color: rebeccapurple;
    }
    .b{
        height: 100vh;   
        background-color: aqua;
    }
    </style>
</head>
<body>
    <div class="a"></div>
    <div class="b"></div>
    <script>
    document.addEventListener("wheel",(e)=>{
        console.log(document.scrollingElement.scrollTop);
        console.log(document.scrollingElement.clientHeight);
        console.log(document.scrollingElement.scrollHeight);
        if(document.scrollingElement.scrollTop+document.scrollingElement.clientHeight==document.scrollingElement.scrollHeight){
        console.log("123");
        }
    })
    </script>
</body>
</html>

效果:
在这里插入图片描述

注意事项:
1.断判要放在滚轮事件中否则只判断一次发现不行然后就没了


参考文章:
使用document.scrollingElement控制窗体滚动高度
Element.scrollHeight
Element.clientHeight
彻底搞懂clientHeight、offsetHeight、scrollHeight的区别

### 回答1: 可以通过以下方式判断iframe内的页面是否滚动到底部: 1. 获取iframe元素 ```javascript const iframe = document.getElementById('my-iframe') ``` 2. 获取iframe内部的document对象 ```javascript const iframeDoc = iframe.contentDocument || iframe.contentWindow.document ``` 3. 判断iframe内部的页面是否滚动到底部 ```javascript const isIframeScrollToBottom = iframeDoc.documentElement.scrollHeight - iframeDoc.documentElement.scrollTop === iframeDoc.documentElement.clientHeight ``` 如果isIframeScrollToBottom为true,则表示iframe内部的页面已经滚动到底部。 ### 回答2: 在Vue中,我们可以通过以下步骤来判断iframe内的页面是否滚动到底部: 1. 首先,我们需要获取到iframe元素,可以通过`document.getElementById`来获取iframe元素的引用。 2. 接下来,我们可以通过`contentWindow`属性来获取iframe内部窗口的引用,以便后续操作。 3. 通过`contentWindow.document.documentElement.scrollHeight`和`contentWindow.document.documentElement.clientHeight`属性,可以得到iframe内部文档的总高度和显示窗口的高度。 4. 判断滚动高度是否等于(或大于)文档总高度减去显示窗口的高度,即`contentWindow.document.documentElement.scrollTop >= (contentWindow.document.documentElement.scrollHeight - contentWindow.document.documentElement.clientHeight)`。 5. 如果上述条件成立,则表示iframe内的页面已经滚动到底部。 以下是一个示例的Vue方法,用于判断iframe内的页面是否滚动到底部: ```javascript methods: { checkIframeScroll() { const iframe = document.getElementById('myIframe'); const contentWindow = iframe.contentWindow; const scrollHeight = contentWindow.document.documentElement.scrollHeight; const clientHeight = contentWindow.document.documentElement.clientHeight; const scrollTop = contentWindow.document.documentElement.scrollTop; if (scrollTop >= (scrollHeight - clientHeight)) { console.log('滚动到底部'); } else { console.log('未滚动到底部'); } } } ``` 请注意,以上示例中的`myIframe`是根据iframe元素的id属性来获取引用的,您需要根据实际情况进行调整。另外,示例中的判断方法是在某个Vue方法中调用的,您可以根据需求来触发该方法。 ### 回答3: 在Vue中判断iframe内的页面是否滚动到底部,可以通过以下步骤实现: 1. 在Vue组件中,使用ref属性给iframe元素添加一个唯一的引用名称,例如ref="myIframe"。 2. 在mounted()生命周期钩子函数中获取该iframe元素,并添加滚动事件监听器。 3. 在滚动事件监听器中判断iframe内页面的滚动位置是否已经到达底部。 具体代码如下: ```javascript <template> <div> <iframe ref="myIframe" src="https://example.com"></iframe> </div> </template> <script> export default { mounted() { // 获取iframe元素并添加滚动事件监听器 const iframe = this.$refs.myIframe; iframe.addEventListener("scroll", this.checkScrollBottom); }, methods: { checkScrollBottom() { // 判断iframe内页面是否到达底部 const iframe = this.$refs.myIframe; const scrollTop = iframe.scrollTop; // 滚动距离 const scrollHeight = iframe.scrollHeight; // 内容高度 const clientHeight = iframe.clientHeight; // 可视区域高度 if (scrollTop + clientHeight >= scrollHeight) { console.log("已滚动到底部"); // 执行滚动到底部后的操作 } }, }, }; </script> ``` 注意:如果iframe内页面和外部页面不在同一个域,可能会受到同源策略的限制,无法直接获取内部页面的滚动位置。在此情况下,可以使用postMessage方法进行跨域通信,让内部页面向外部页面发送滚动位置信息,然后在外部页面中进行判断
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值