javascript jQuery滚动到元素底部而不是顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17758422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:30:45  来源:igfitidea点击:

jQuery scroll to bottom of element not top

javascriptjqueryscrollsmooth-scrolling

提问by ramo

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?

上面的代码在滚动到浏览器窗口时将元素放置在浏览器窗口的顶部,有没有办法可以滚动到以浏览器窗口底部元素结尾的滚动元素?

采纳答案by Jason Sperske

You could use the height of the windowto calculate your scroll position

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

回答by Khanh TO

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