[英]Javascript Scroll event
在某些博客上,當您滾動到頁面底部時,它們會在頁面右下角顯示一個 DIV。
很多時候在博客文章中,一旦您將頁面向下移動到評論部分開始的位置,他們就會將此 DIV 滑入視圖。
我正在嘗試復制這個,我看到一個網站這樣做,但它沒有在評論附近這樣做,而是使用下面的代碼,您可以在中間看到它。
Document height - the Window height / 2
所以它實際上留在了頁面的一半位置。 當我到達頁面的評論部分時,我 go 如何讓它進入視圖,假設我的評論包含在帶有 ID comments
的 DIV 中
$(document).scroll(function () {
var curPos = $(document).scrollTop();
var docHeight = $(document).height() - $(window).height();
if (curPos > (docHeight / 2)) {
MoneyBox.show();
} else {
MoneyBox.hide();
}
});
嘗試比較 scrollTop 和 div 的偏移量
$(document).scroll(function(){
var curPos = $(document).scrollTop();
var commentsPos = $('#comments').offset().top;
if(curPos >= commentsPos) {
MoneyBox.show();
} else {
MoneyBox.hide();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.