javascript jQuery - 在 slideDown 上滚动到 div 的底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18600682/
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
jQuery - Scroll to bottom of div on slideDown
提问by user1502223
Im trying to scroll to the bottom of the div when the user clicks on a link to slide the div down.
当用户单击链接将 div 向下滑动时,我试图滚动到 div 的底部。
I've tried using the scrollTo and animate
我试过使用 scrollTo 和动画
$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);
but nothing happens
但什么也没发生
heres the fiddle
这是小提琴
回答by mtt
Demo: http://jsfiddle.net/CLzfW/4/
演示:http: //jsfiddle.net/CLzfW/4/
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$('html, body').animate({
scrollTop: 1000
}, 2000);
});
Just use your .expander height.
If you need this to be variable you can do this: http://jsfiddle.net/CLzfW/26/
只需使用您的 .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);
});
回答by Mina Gabriel
USE :
利用 :
$('html, body').animate({scrollTop: $(document).height()}, 'slow');
UPDATE
更新
Use this ,
The trick is in scrollHeight
see my answer here How do I get the “auto” height of a DIV
使用这个,诀窍是在scrollHeight
这里看到我的答案如何获得 DIV 的“自动”高度
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$('.expander ').animate({scrollTop: $('.expander')[0].scrollHeight}, 'slow');
$('html, body').animate({scrollTop: $(document).height()}, 'slow');
});
回答by Ronny Kibet
try this instead.
试试这个。
$('.button').click(function(e){
e.preventDefault();
$('.expander').slideToggle();
$("html, body").animate({ scrollTop: $(document).height() }, 2000);
});
回答by Sualkcin
$("#something").click(function() {
$('html, body').animate({
scrollTop: $("#element").offset().top
}, 1000);
});
You are using a class name instead of an id. You must scroll to a unique element.
您使用的是类名而不是 id。您必须滚动到一个唯一的元素。