Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Cómo recorrer la función jQuery .next none -top

Estoy tratando de recorrer y desplazar automáticamente cada elemento de un grupo de elementos a la parte superior de la página con la función jQuery .next . Sin embargo, la función desplaza solo el primer elemento, se detiene y no se repite.

 $(function() { setInterval(function() { var $next = $('.per-account:first').next('.per-account'); var $first = $('.per-account:first'); if ($next.length) { $('html, body').animate({ scrollTop: $next.offset().top }, 1000); } else { $('html, body').animate({ scrollTop: $first.offset().top }, 1000); } }, 3000) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="per-account" style="height: 500px; width: 100%; background: red;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: blue;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: orange;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: green;"> </div>

el primer elemento y se detiene. Cualquier idea de lo que estoy haciendo mal aquí.

Mi código se ve a continuación:

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Siempre obtienes el siguiente elemento después de :first , lo que significa que siempre es el segundo elemento, no el bucle.

Agregue una clase al elemento actual y luego obtenga el siguiente elemento después de eso.

 $(function() { setInterval(function() { var $current = $('.per-account.current') var $next = $current.next('.per-account'); if (!$next.length) { // wrap around to the beginning $next = $('.per-account:first'); } $current.removeClass('current'); $next.addClass('current'); $('html, body').animate({ scrollTop: $next.offset().top }, 1000); }, 3000) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="per-account current" style="height: 500px; width: 100%; background: red;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: blue;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: orange;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: green;"> </div>

about 4 years ago · Juan Pablo Isaza Report

0

El siguiente código hará que funcione. Empezabas desde el primero en cada intervalo.

 $(function() { function runScroll(){ let $current = $('.per-account:first'); const id = setInterval(runInterval, 1000); function runInterval(){ if ( $current.next('.per-account').length ){ $current = $current.next('.per-account'); $('html, body').animate({ scrollTop: $current.offset().top }, 500); } else { clearInterval(id); } } } runScroll(); // Re-run to scroll again });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="per-account" style="height: 500px; width: 100%; background: red;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: blue;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: orange;"> </div> <div class="per-account" style="height: 500px; width: 100%; background: green;"> </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!