jQuery:跳到下一个<section>

dco*_*bus 1 css jquery html5

所以这应该是相当基础的...我正在做,但我想要几个不同的选择.

一种选择是使用"平滑滚动"和锚名称......但我发现它非常不一致.

这是我的HTML结构:

<section id="home">
<!-- some content -->
</section>

<section id="about">
<!-- some content -->
</section>

<section id="services">
<!-- some content -->
</section>

...
Run Code Online (Sandbox Code Playgroud)

我在该部分的右侧有一些"快速按钮",基本上允许您从一个部分向上或向下"移动".

Ali*_*guy 5

jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

// Scroll to "about" section
$('#about').scrollTo('fast', 'linear');
Run Code Online (Sandbox Code Playgroud)

更新 - 要从一个部分跳到另一个部分,请使用简单的事件处理程

JQuery的:

$('.next-section').click(function(){
    var $this = $(this),
        $next = $this.parent().next();

    $next.scrollTo($next.offset().top, 500, 'linear');
});

$('.prev-section').click(function(){
    var $this = $(this),
        $prev = $this.parent().prev();

    $prev.scrollTo($prev.offset().top, 500, 'linear');
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<section id="about">
    <a href="#" class="prev-section">Previous Section</a>
    <a href="#" class="next-section">Next Section</a>
    <div class="section-content">
        Foobar
    </div>
</section>
Run Code Online (Sandbox Code Playgroud)

这是一个演示:http://jsfiddle.net/AlienWebguy/Xdg2k/