【发布时间】:2013-07-19 11:12:40
【问题描述】:
我正在使用以下内容滚动到一个元素
$("html, body").animate({
scrollTop: $('selector').offset().top
}, 500);
上面的代码将元素滚动到浏览器窗口的顶部,有没有办法可以滚动到滚动到浏览器窗口底部的元素结束的元素?
【问题讨论】:
标签: javascript jquery scroll smooth-scrolling
我正在使用以下内容滚动到一个元素
$("html, body").animate({
scrollTop: $('selector').offset().top
}, 500);
上面的代码将元素滚动到浏览器窗口的顶部,有没有办法可以滚动到滚动到浏览器窗口底部的元素结束的元素?
【问题讨论】:
标签: javascript jquery scroll smooth-scrolling
尝试这样的方法将滚动条放在元素的底部
$("html, body").animate({
scrollTop: $('selector').offset().top + $('selector').outerHeight(true)
}, 500);
或者这样将元素放在滚动的底部:
$("html, body").animate({
scrollTop: $('selector').offset().top + $('selector').outerHeight(true) -$(window).height()
}, 500);
【讨论】:
$("html, body").animate({ scrollTop: $('selector').offset().top - ($(window).height()-$("selector").outerHeight() +10)}, 500);
您可以使用window 的高度来计算您的滚动位置
【讨论】: