简体   繁体   English

滚动到下一个div

[英]Scroll to next div

I've got the following code. 我有以下代码。 At the moment, it only scrolls down the height of a variable. 目前,它仅向下滚动变量的高度。

http://jsfiddle.net/tmyie/gF6U3/1/ http://jsfiddle.net/tmyie/gF6U3/1/

$('.col100').click(function(e){
    e.preventDefault();
    var H = $('.col100').outerHeight();
    $('html, body').animate({scrollTop: H}, 200);   
});

Questions: 问题:

  • Why is it only scrolling once? 为什么只滚动一次?
  • Could it be better implemented: so it scrolls through each sibling with each click? 能否更好地实现:每次点击都能滚动浏览每个兄弟姐妹?

You are scrolling to the same position each time. 您每次都滚动到同一位置。 Easiest would be to calculate the offset position of the next box: 最简单的方法是计算下一个框的偏移位置:

http://jsfiddle.net/B472g/ http://jsfiddle.net/B472g/

$('.col100').click(function(e){
    e.preventDefault();
    var H = $(this).next().offset().top;
    $('html, body').animate({scrollTop: H}, 200);   
});

Note that this doesn't handle what to do at the last box.. depends on how you want to implement it. 请注意,这不会处理最后一个框的操作。.取决于您要如何实现它。

use this - http://jsfiddle.net/gF6U3/3/ 使用这个-http://jsfiddle.net/gF6U3/3/

$('.col100').click(function(e){
    e.preventDefault();
    var H = $(this).next('.col100').offset().top;
    $('html, body').animate({scrollTop: H}, 200);
});

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

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