如何滚动到单击时离开屏幕的div

时间:2015-08-13 17:09:28

标签: javascript jquery css

http://jsfiddle.net/qg9hv5g7/3/

$(".button").click(function() {
    $(this).parent().toggleClass("b");   
});
.a {
  height: 200px;
  width: 150px;
  float: left;
  background: red;
  margin-left: 10px;
}
.b {
  height: 200px;
  width: 300px;
  float: left;
  background: blue;
  margin-left: 10px;
}

当我点击第9个div时,它会离开屏幕,当点击它时,如何自动滚动到div的新位置

1 个答案:

答案 0 :(得分:1)

此链接有帮助吗?它显示了滚动到特定元素的各种方法。

http://www.robertprice.co.uk/robblog/2013/02/using-jquery-to-scroll-to-an-element/

我创建了一个jsfiddle以供参考:

http://jsfiddle.net/dboots/kg3d4Ly8/

function scrollToElement(el) {
    $('html, body').animate({
        scrollTop: ($(el).offset().top)
    },500);
}