9

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?

ramo
  • 945
  • 4
  • 12
  • 20

2 Answers2

17

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);
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • 9
    worked with some modification: `$("html, body").animate({ scrollTop: $('selector').offset().top - ($(window).height()-$("selector").outerHeight() +10)}, 500);` – ramo Jul 20 '13 at 04:29
4

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

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124