繁体   English   中英

Javascript 滚动事件

[英]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.

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