繁体   English   中英

滚动到页面javascript的某个高度

[英]Scroll to certain height of a page javascript

我有一个非常简单的一行代码,用于检查用户是否已滚动到页面底部,我想稍微更改一下并查找用户是否已到达页面的页脚。 页脚的高度有点350px。

这是我的代码:

if($(window).scrollTop() + $(window).height() == ($(document).height())
{
...
}

这就像一个魅力(在滚动事件中加载更多内容),但如果我喜欢这样:

if($(window).scrollTop() + $(window).height() == ($(document).height()-350))

这不起作用。 当我尝试alert('$(document).height()-350') 它提供了一个完美的警报。

谁能说我做错了什么?

你可能一次滚动超过1个像素,只是跳过相等点。 使它成为> =它应该工作:

if($(window).scrollTop() + $(window).height() >= ($(document).height()-350))

尝试

if($(window).scrollTop() + $(window).height() >= $(document).height()-350)

你还有'('$'前面的$(文件).height()需要删除

您希望使用>=而不是== ,否则您必须在滚动中使用像素完美才能触发事件。

试一试。 您可以使用页脚元素的.offset().top来获取相对于文档的Y位置。

var scrollBottom = $(window).scrollTop() + $(window).height();
var footerTop = $('#footer').offset().top; // change selector as needed

if ( scrollBottom >= footerTop ) {
  alert("hello footer");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM