【发布时间】:2013-09-04 04:55:21
【问题描述】:
当用户单击链接以向下滑动 div 时,我试图滚动到 div 的底部。
我尝试过使用 scrollTo 和动画
$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);
但是什么都没发生
这是小提琴
【问题讨论】:
标签: javascript jquery html css
当用户单击链接以向下滑动 div 时,我试图滚动到 div 的底部。
我尝试过使用 scrollTo 和动画
$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);
但是什么都没发生
这是小提琴
【问题讨论】:
标签: javascript jquery html css
演示:http://jsfiddle.net/CLzfW/4/
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$('html, body').animate({
scrollTop: 1000
}, 2000);
});
只需使用您的 .expander 高度。
如果你需要它是可变的,你可以这样做:http://jsfiddle.net/CLzfW/26/
var scroll_to = $('.expander').offset().top + $('.expander').height();
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$('html, body').animate({
scrollTop: scroll_to
}, 2000);
});
【讨论】:
使用:
$('html, body').animate({scrollTop: $(document).height()}, 'slow');
更新
使用这个,
诀窍在于scrollHeight 在这里查看我的答案How do I get the “auto” height of a DIV
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$('.expander ').animate({scrollTop: $('.expander')[0].scrollHeight}, 'slow');
$('html, body').animate({scrollTop: $(document).height()}, 'slow');
});
【讨论】:
$("#something").click(function() {
$('html, body').animate({
scrollTop: $("#element").offset().top
}, 1000);
});
您使用的是类名而不是 id。您必须滚动到唯一的元素。
【讨论】:
试试这个。
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$("html, body").animate({ scrollTop: $(document).height() }, 2000);
});
【讨论】: