jQuery滚动到类的下一个实例

问题描述 投票:0回答:2

所以我只想从当前可见的类滚动到该类的下一个实例。

HTML看起来像这样

<div class="container"> 
 <div class="section">
` <p> lorem ipsum </p>
  <a href="javascript:void(null)" class="next"> next section </a>
 </div>
<div>

^重复'x'次]

我的js:

$(.next).click(function(e){
 $('html,body').animate({}, 
  scrollTop: $(this).parent().next().offset().top
  },'slow');

});

很显然,该代码无法正常工作,因为它只会带您进入下一个div,但我是新手,非常感谢您的帮助。

小提琴:http://jsfiddle.net/o1wjedd5/

javascript jquery scroll scrollto scroller
2个回答
2
投票

您需要使用.closest(".container")才能到达父容器。然后使用.next()


3
投票

删除动画方法开始时的},

© www.soinside.com 2019 - 2024. All rights reserved.