繁体   English   中英

jQuery - 在slideDown上滚动到div的底部

[英]jQuery - Scroll to bottom of div on slideDown

当用户点击链接滑动div时,我试图滚动到div的底部。

我尝试过使用scrollTo和animate

$('html, body').animate({
scrollTop: $("#elementID").offset().top
}, 2000);

但没有任何反应

这是小提琴

http://jsfiddle.net/CLzfW/29/

演示: 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);
});

DEMO

采用 :

$('html, body').animate({scrollTop: $(document).height()}, 'slow');

UPDATE

滚动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');
});

滚动基于div滚动高度

$("#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);
});

演示http://jsfiddle.net/CLzfW/17/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM