Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to next section

My code looks like this:

<div id="arrow">
    <a class="next"></a>
    <a class="previous"></a>
</div>

<section id="first">
...
</section>

<section id="second">
...
</section>

<section id="third">
...
</section>

The element #arrow has position: fixed, and I'm trying to make the window scroll to the next section when a.next is clicked.

Ex: The first time a.next is clicked, the window scrolls to section#first, the second time, the window scrolls to section#second, etc. The same thing happens to a.previous.

Does someone know how to solve this problem?

Thanks a lot!


EDIT

My JS code:

$('#arrows a.previous').click(function() {
    $.scrollTo($(this).closest('section').prev(),800);
});

$('#arrows a.next').click(function() {
    $.scrollTo($(this).closest('section').next(),800);
});     
like image 397
Matheus Souza Avatar asked Mar 17 '23 08:03

Matheus Souza


1 Answers

You will need to handle to 3 events in this case:

  1. Current page position - updated each time.
  2. User scrolls manualy the page.
  3. User clicks the prev or next button.

2, 3 need to use the current page position and update him according to the direction that the page is scrolling.

My quick demos : Vertical Version jsFiddle --- Horizontal Version jsFiddle

Vertical Version snippet :

$(function(){
    
    var pagePositon = 0,
        sectionsSeclector = 'section',
        $scrollItems = $(sectionsSeclector),
        offsetTolorence = 30,
        pageMaxPosition = $scrollItems.length - 1;
    
    //Map the sections:
    $scrollItems.each(function(index,ele) { $(ele).attr("debog",index).data("pos",index); });

    // Bind to scroll
    $(window).bind('scroll',upPos);
    
    //Move on click:
    $('#arrow a').click(function(e){
        if ($(this).hasClass('next') && pagePositon+1 <= pageMaxPosition) {
            pagePositon++;
            $('html, body').stop().animate({ 
                  scrollTop: $scrollItems.eq(pagePositon).offset().top
            }, 300);
        }
        if ($(this).hasClass('previous') && pagePositon-1 >= 0) {
            pagePositon--;
            $('html, body').stop().animate({ 
                  scrollTop: $scrollItems.eq(pagePositon).offset().top
              }, 300);
            return false;
        }
    });
    
    //Update position func:
    function upPos(){
       var fromTop = $(this).scrollTop();
       var $cur = null;
        $scrollItems.each(function(index,ele){
            if ($(ele).offset().top < fromTop + offsetTolorence) $cur = $(ele);
        });
       if ($cur != null && pagePositon != $cur.data('pos')) {
           pagePositon = $cur.data('pos');
       }                   
    }
    
});
section { min-height:800px; }
#arrow {
    position:fixed; 
    right:0; 
    top:0;
    background-color:black;
    color:white;
}
#arrow a{
    display:inline-block;
    padding:10px 20px;
    cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="arrow">
    <a class="next">next</a>
    <a class="previous">prev</a>
</div>
<section style="background-color:green">...</section>
<section style="background-color:blue">...</section>
<section style="background-color:red">...</section>
like image 158
Shlomi Hassid Avatar answered Mar 18 '23 23:03

Shlomi Hassid