.next () jQuery only scroll once

Your best bet is to check fiddle and try and push buttons.

The problem I am facing is the first button to go to the next section using a jQuery function .next()

. However, it won't work for the rest.

var buttons= document.querySelectorAll(".next-section");

for(var i=0; i<buttons.length; i++){
    buttons[i].addEventListener("click", scrollDown);
}   

function scrollDown(){
    console.log("Debug: BUTTON CLICKED")
    $('html, body').animate({
        scrollTop: $("section").next(".page").offset().top 
    }, 'slow');
}

      

+3


source to share


1 answer


The application runs in the application and the method scrollDown

is called.

However, it $("section").next(".page")

always returns the second page and always tries to scroll the page to the second section.

What's happening:

  • $("section")

    returns all elements of section

    your page (pages 1, 2, 3, 4)
  • .next(".page")

    returns next page for each item (pages 2, 3, 4)
  • .offset().top

    returns the top position of the first element in the list (page 2)

Instead, you need to get the next page of the current page that is $(this).closest(".page")

, and get its offset.
You can use the following code instead:



$('html, body').animate({
    scrollTop: $(this).closest(".page").next().offset().top 
}, 'slow');

      

Here's a working demo:

var buttons = document.querySelectorAll(".next-section");

for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", scrollDown);
}

function scrollDown() {
  console.log("Debug: BUTTON CLICKED")
  $('html, body').animate({
    scrollTop: $(this).closest(".page").next().offset().top
  }, 'slow');
}
      

html,
body {
  text-align: center;
  margin: 0;
}

.page {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.content {
  display: block;
  height: auto;
}

#one {
  background-color: grey;
}

#two {
  background-color: red;
}

#three {
  background-color: blue;
}
      

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="./css/styles.css">
</head>

<body>

  <section class="page" id="one">
    <div class="content">
      <h1>Page1</h1>
      <p>This is page 1</p>
      <button class="next-section">Next Page!</button>
    </div>
  </section>

  <section class="page" id="two">
    <div class="content">
      <h2>PAGE 2</h2>
      <button class="next-section">Next Page</button>
    </div>
  </section>

  <section class="page" id="three">
    <div class="content">
      <h2>PAGE 3</h2>
      <button class="next-section">Next Page</button>
    </div>
  </section>

  <section class="page" id="four">
    <div class="content">
      <h2>PAGE 4</h2>
      <button class="next-section">Next Page</button>
    </div>
  </section>





  <script type="text/javascript" src="./js/script.js"></script>
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</body>

</html>
      

Run codeHide result


One small suggestion for improvement: Don't mix JS events with vanilla and jQuery.
If you are using jQuery use and $(".next-section").click(scrollDown)

instead .document.querySelectorAll

addEventListener

+4


source







All Articles