js 监听页面滚动到底部,监听可视区域滚动到底部

135 篇文章 3 订阅
44 篇文章 0 订阅

全局页面body上的底部监听:

window.onscroll = function(){
            var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
            var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
            var scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight;
            if(scrollTop+windowHeight>=scrollHeight){   //考虑到滚动的位置一般可能会大于一点可滚动的高度,所以这里不能用等于
                console.log("距顶部"+scrollTop+"可视区高度"+windowHeight+"滚动条总高度"+scrollHeight);
            }   
        }

局部可视区域的滚动到底部(vue版本代码):

<div style="flex:1;overflow-y:auto;" @scroll="scrollEvent">内容</div>
methods: {
        // 滚定监听
        scrollEvent(e) {
            // console.log('滚动',e.currentTarget.scrollTop,e.currentTarget.clientHeight,e.currentTarget.scrollHeight);
            if (Math.ceil(e.currentTarget.scrollTop + e.currentTarget.clientHeight) >=e.currentTarget.scrollHeight) {   //容差:20px
                // console.log('滚动到底部');
                this.$emit('scrollBottom');
            }
        }
    },

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答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方法进行跨域通信,让内部页面向外部页面发送滚动位置信息,然后在外部页面中进行判断。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

草字

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

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

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

打赏作者

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

抵扣说明:

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

余额充值