【问题标题】:jQuery: Hide element if user scrolled to the end of the pagejQuery:如果用户滚动到页面末尾则隐藏元素
【发布时间】:2023-02-23 00:53:56
【问题描述】:

如果用户滚动到页面上的某个位置,我想显示一个元素。 但我也想在用户到达页面末尾时隐藏该元素。

现在我只有显示元素的代码:

( function( $ ) {

    $(document).scroll(function() {
        var y = $(this).scrollTop();
        if (y > 800) {
            $('.sticky-bottom').fadeIn();
        } else {
            $('.sticky-bottom').fadeOut();
        }

    });

} ( jQuery ) );

但是我怎样才能把它隐藏在页面的末尾呢?完全不像.scrollBottom

【问题讨论】:

    标签: javascript html jquery scrolltop


    【解决方案1】:

    关于什么

    $(document).scroll(function() {
    
        if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
            // you're at the bottom of the page
            $('.sticky-bottom').fadeIn();
        } else {
            $('.sticky-bottom').fadeOut();
        }
    
    });
    

    【讨论】:

    • 如果我更改 fadeInfadeOut 看起来不错。但现在它从一开始就可见。但我想在滚动几个像素后显示它
    • @Cray 您可以在条件公式中添加一些像素,例如 (innerheight - your_anticipation)
    猜你喜欢
    • 2012-05-26
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多