質問

Good evening everybody! Currently, on the site I'm working on (http://bit.ly/1eGCShX), I've got it to scroll down to each div all the way down, however after you've finished and click on the last one again, it doesn't scroll back all the way to the top (instead the previous div) and that's what I'm trying to accomplish.

However, I need it to repeat itself over again to start back from the first div, because if you finish it once and scroll up to click the first one again it stars where it left off. I've been messing with this for awhile and couldn't get it. Any help is kindly appreciated! Here is my JS;

  $('div.section').first();

  $('a.display').on('click', function(e) {
      e.preventDefault();

        var t = $(this).text(),
        that = $(this);

      if ($('.currentPanel').next('div.section').length > 0) {
          var $next = $('.currentPanel').next('.section');
          var top = $next.offset().top;

          $('.currentPanel').removeClass('currentPanel');     
          $(function () {
                 $next.addClass('currentPanel');
                 $('html, body').animate({scrollTop: $('.currentPanel').offset().top }, 'slow');

          });
    } else if ($('.currentPanel').prev('div.section').length > 0) {
          var $prev = $('.currentPanel').prev('.section');
          var top = $prev.offset().top;

          $('.currentPanel').removeClass('currentPanel');

          $(function () {
                 $prev.addClass('currentPanel');
                 $('html, body').animate({scrollTop: $('.currentPanel').offset().top }, 'slow');
          });of
    } 
  });

And of course, the JSFiddle to make things x10 more easier! I've made a simplified version.

http://jsfiddle.net/dylanopet/ADsKH/9/

Again, appreciate for taking your time to read this and wish you all a positive week.

役に立ちましたか?

解決

Where you have

var $prev = $('.currentPanel').prev('.section');

change it to

var $prev = $('.section').eq(0);

You were telling the function you wanted to scroll to the previous element. But you actually want to scroll to the first element.

Working example: http://jsfiddle.net/58ZMZ/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top