簡體   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