How to create a button with Javascript, which when clicking it advances to the next section?

Asked

Viewed 151 times

-2

I have a page with several <section> for each block of content, as in the example below:

<style>
     section {
        width: 100%;
        height: 100vh;
     }

     .seta {
        position: fixed;
        bottom: 0;
        right: 30px;
        color: #000;
        font-size: 72px;
        cursor: pointer;
     }
</style>

<section id="sec-a">
   ...
</section>

<section id="sec-b">
   ...
</section>

<section id="sec-c">
   ...
</section>

<section id="sec-d">
   ...
</section>

<div class="seta">
^
</div>

And on this page, at the bottom right, I’m going to have an arrow that will stay fixed floating over the screen. By clicking this arrow, I want the screen to scroll to the next <section>, this is possible using Javascript?

  • as sec will be hidden?

  • @Pedrohenrique no, each section will occupy 100% of the screen size.

  • He just moves on, doesn’t come back?

  • Yeah, it just keeps going.

1 answer

1


It is possible to do using a counter sectionAtual, in this case before performing any event, I counted how many section exist in the DOM, with this I made an event click on the arrow, in that event a check is made if there is a next Section to go to it, if there is no application is stopped, if there is an auto increment in the counting variable sectionAtual, which will be used to go up to the next Section, which in case I recover using the method eq => $('section[id^=sec-]:eq(n)').

When scrolling back, a new position will be set for the sectionAtual, if you’re in section-C and go back to the section-B, will not go to the section-D, the counter will be modified and the next Section will be the section-C

Follow the example:

const quantidadeSections = $("section[id^=sec-]").length; // Quantidade de sections na DOM
let sectionAtual = -1; // Contador de section
let arraySections = []; // Array com posições das sections na DOM

// Recupera posição das sections
setTimeout(() => {
    $("section[id^=sec-]").each(function(){
        arraySections.push($(this).position().top)
    });
}, 1000);


// Evento de click na seta
$('.seta').click(function(){
    // Verifica se é a ultima section
    if((sectionAtual + 1) === quantidadeSections) return false;

    sectionAtual++;
    $('html,body').animate({
        scrollTop: $(`section[id^=sec-]:eq(${sectionAtual})`).offset().top
    }, 'slow');
});

// Evento ao rolar a scroll
$(document).scroll(function () { // oscultador de scroll
    const posicaoScroll = $(document).scrollTop(); // obtem a quantidade de scroll no momento
    for (let countSec = 0; countSec < quantidadeSections; countSec++) {
        
        if(posicaoScroll >= arraySections[countSec]) sectionAtual = countSec;
        
    }
})
section {
    width: 100%;
    height: 100vh;
}

.seta {
    position: fixed;
    bottom: 0;
    right: 30px;
    color: #000;
    font-size: 72px;
    cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="sec-a">
    SECTION A
</section>

<section id="sec-b">
    SECTION B
</section>

<section id="sec-c">
    SECTION C
</section>

<section id="sec-d">
    SECTION D
</section>

<div class="seta">
    ^
</div>


Reference:

eq()

Animate()

Selectors

  • It works well, but has two bugs: the first is that here in my he is skipping the first section do not know pq. The second is that if I use the scroll bar to go back to the previous section the arrow no longer works.

  • 1

    So, the one about the scroll bar, didn’t inform the question. Do you want that when the scroll bar is returned it also returns the sections? If I’m in section#C and return to the sectiob#B and click to go to the next, you have to go to the section#C again, not for the section#D , that?

  • Exactly. Here the simple act of scrolling the bar already hangs the function, if only solve this already solves my problem.

  • 1

    I get it, just a moment, I’ll adjust this detail.

  • 1

    @Mateusd. code changed.

  • worked cool! The only caveat is that you need to give 2 clicks on the first try for the code to start running. But it still works for what I need.

  • 1

    I noticed that here in S.O needs two click, it seems that the top where the result is displayed is not the real top, there is something between the first element and the top, so in the first click it goes to the element, note!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.