Opposite of “scrollTop” in jQuery

前端 未结 8 946
情深已故
情深已故 2020-12-14 08:04

jQuery has a function called scrollTop which can be used to find the number of pixels hidden above the current page view.

I\'m not really sure why, but there is no s

相关标签:
8条回答
  • This is how I have calculated the distance from the bottom of the element to the bottom of the document:

    $(document).height() - ($('#element').offset().top + $('#element').height());
    
    0 讨论(0)
  • 2020-12-14 08:07

    Perhaps this will help how to tell jquery to scroll to bottom, because there is really no opposite function. Same is the problem with scrollLeft - there is no scrollRight

    0 讨论(0)
  • 2020-12-14 08:07

    try this:

    return this[0].scrollHeight - this.scrollTop() - this.height();
    
    0 讨论(0)
  • 2020-12-14 08:08

    As scrollTop's default behaviour scrolls to 0 when passing a negative value, I did this function that handles scrollTop and simulate a "scrollDown".

    If anchor_pos is negative (so it's above my current scroll position), I subtract its value from current scroll position (as it has a negative value, I'm using + sign)

    function jumpToAnchor(scrollable_div_selector, anchor_selector)
    {
        anchor_pos = $(anchor_selector).position().top;
    
        //check if negative number
        if (anchor_pos < 0)
        {
            anchor_pos = $(scrollable_div_selector).scrollTop() + anchor_pos; //anchor_pos is negative, so i'm substracting it
        }
    
        $(scrollable_div_selector).scrollTop(anchor_pos);
    }
    
    0 讨论(0)
  • 2020-12-14 08:15

    You could make a pretty simple plugin for this:

    $.fn.scrollBottom = function() { 
      return $(document).height() - this.scrollTop() - this.height(); 
    };
    

    Then call it on whatever element you wanted, for example:

    $(window).scrollBottom();  //how many pixels below current view
    $("#elem").scrollBottom(); //how many pixels below element
    
    0 讨论(0)
  • 2020-12-14 08:30
    function scrollBottom()
    {
        return $( window ).scrollTop() + $( window ).height();
    }
    
    // example usage
    $( '#footer' ).css( 'top', scrollBottom() - $( '#footer' ).height() );
    
    0 讨论(0)
提交回复
热议问题