Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to next class element

How can I get scroll to the next class element called section and vice versa?

Below are the code i have tried so far:-

<div class="section">
  content
</div>
<div class="hero">
  content
</div>
<div class="midPage">
  content
</div>
<div class="section">
  content
</div>
<!-- Scroll Buttons -->
<div class="prev">
 <button class="prevButton">Prev</button>
</div>
<div class="next">
 <button class="nextButton">Next</button>
</div>
like image 711
Jose Avatar asked Feb 11 '26 11:02

Jose


1 Answers

Check this fiddle

I did it fast, only next button working, not the best approach.

Check the data- attributes and how is working, i repeat, this is not the best approach.

$(".nextButton").on('click', function(e) {

  var dataGoTo = $(this).attr("data-section");
  var next = $(dataGoTo).attr("data-next");

  $('html, body').animate({
    scrollTop: $(dataGoTo).offset().top
  }, 500);

  $(this).attr("data-section", next);
});
like image 171
Slico Avatar answered Feb 14 '26 00:02

Slico