Getting the scrollbottom using jQuery

后端 未结 2 1572
梦谈多话
梦谈多话 2021-01-14 05:26

I have the following code which gets the amount the user has scrolled from the top and the bottom and then using these values it should hide or show the shadows.

<         


        
相关标签:
2条回答
  • 2021-01-14 05:37

    $(window).height(); // returns height of browser viewport

    $(document).height(); // returns height of HTML document

    Change your code to:

    $(document).ready(function() {
    
     if ($(window).scrollTop() + $(window).height() >= $(document).height() - 15) {
        $('div.shadow-bottom').show();
     }
    
     $(window).scroll(function() {
    
        if ($(window).scrollTop() >= 15) {
            $('div.shadow-top').show();
        } else {
            $('div.shadow-top').hide();
        }
        if ($(window).scrollTop() + $(window).height() >= $(document).height() - 15) {
            $('div.shadow-bottom').show();
        } else {
            $('div.shadow-bottom').hide();
        }
    
     });
    
    });​
    
    0 讨论(0)
  • 2021-01-14 05:54

    The correct working example is:

    $(document).ready(function() {
    
                 if ($(window).height() < $(document).height()) {
                    $('div.shadow-bottom').show();
                 }
    
                 $(window).scroll(function() {
    
                    if ($(window).scrollTop() >= 15) {
                        $('div.shadow-top').show();
                    } else {
                        $('div.shadow-top').hide();
                    }
                    if ($(window).scrollTop() + $(window).height() >= $(document).height() - 15) {
                        $('div.shadow-bottom').hide();
                    } else {
                        $('div.shadow-bottom').show();
                    }
    
                 });
    
                });
    

    Which is based on bhb's answer above.

    0 讨论(0)
提交回复
热议问题