Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery scroll to bottom of element not top

I am using the following to scroll to an element

$("html, body").animate({
  scrollTop: $('selector').offset().top
}, 500);

The above code places the element at the top of the browser window when scrolled to it, is there a way I can scroll to the element with the scroll ending with the element at the bottom of the browser window?

like image 489
ramo Avatar asked Jul 20 '13 03:07

ramo


2 Answers

Try something like this to put the scroll at the bottom of the element

$("html, body").animate({
      scrollTop: $('selector').offset().top + $('selector').outerHeight(true)
    }, 500);

Or this to put the element at the bottom of the scroll:

$("html, body").animate({
          scrollTop: $('selector').offset().top + $('selector').outerHeight(true) -$(window).height()
        }, 500);
like image 108
Khanh TO Avatar answered Oct 16 '22 05:10

Khanh TO


You could use the height of the window to calculate your scroll position

like image 29
Jason Sperske Avatar answered Oct 16 '22 04:10

Jason Sperske