[英]jQuery - Scroll to bottom of div on slideDown
當用戶點擊鏈接滑動div時,我試圖滾動到div的底部。
我嘗試過使用scrollTo和animate
$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);
但沒有任何反應
這是小提琴
演示: 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');
UPDATE
使用此,訣竅在scrollHeight
查看我的答案如何獲得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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.