简体   繁体   中英

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?

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);

您可以使用窗口的高度来计算滚动位置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM