JS判断是否到达页面底部

JS判断是否到达页面底部

// 判断标准:窗口高度 + 滚动条位置 >= 页面高度

/* -------------------------------------------- */
 
// 原生JS 判断整个文档滚动至底部
window.onscroll = ()=>{
  // 窗口高度
  var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
	// 页面高度
	var documentHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
	// 滚动条位置
	var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
	if ((windowHeight + scrollTop + 2) >= documentHeight) {
		console.log('页面滚动到达底部');
	}
}

// 原生JS 判断元素内部滚动至底部
document.getElementsByClassName('rank_body')[0].getElementsByTagName('ul')[0].onscroll = (e)=>{
  if(e.target.clientHeight + e.target.scrollTop + 2 >= e.target.scrollHeight){
    console.log('元素内部页面滚动到达底部');
  }
}
// or
document.getElementsByClassName('rank_body')[0].getElementsByTagName('ul')[0].addEventListener("scroll", (e)=>{
  if(e.target.clientHeight + e.target.scrollTop + 2 >= e.target.scrollHeight){
    console.log('元素内部页面滚动到达底部');
  }
}, false);

/* -------------------------------------------- */
		
//JQ 判断整个文档滚动至底部
$(window).scroll(function() {
  // 窗口高度
  var w_h = parseFloat($(window).height());
	// 页面高度
	var doc_h = $(document).height();
	//当前滚动条位置时,页面可见区域及以上区域高度
	totalheight = w_h + parseFloat($(window).scrollTop()) + 2;
	if (totalheight >= doc_h) {
		console.log('页面滚动到达底部');
	}
});
 
//JQ 判断元素内部滚动至底部
$('#showRankbox').scroll(function() {
  // 元素高度
  var box_h = $('#showRankbox').height();
	// 元素内部页面高度
	var box_doc_h = parseFloat($('#showRankbox').find(' > ul').height());
	var $this = $(this);
	totalheight = box_h + parseFloat($this.scrollTop());
	if (totalheight + 2 >= box_doc_h) {
		console.log('元素内部页面滚动到达底部');
	}
})

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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、付费专栏及课程。

余额充值