Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
jQuery scroll to next element, even if it is not a sibling

I want to scroll to the next column once I click the button. And it is working fine if I'm clicking on the first one, using this JS script:

$(".btn").click(e => {
  let element = $(e.target);
  $([document.documentElement, document.body]).animate({
    scrollTop: element.parents('.col-md-6').next().first().offset().top
  }, 2000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="row">
  <div class="col-md-6">
    <a class="btn">Button 1</a>
  </div>
  <div class="col-md-6">
    <a class="btn">Button 2</a>
  </div>
</div>
<div class="row">
  <div class="col-md-6">
    <a class="btn">Button 3</a>
  </div>
  <div class="col-md-6">
    <a class="btn">Button 4</a>
  </div>
</div>

But what is happening is, it only works for col-md-6 as siblings. Meaning that it would only work in the first button for each row, the second one does nothing because there is no siblings next. I already searched everywhere on internet, trying to find a way to get the next col-md-6 whether it is a sibling or not so I can scroll to it, but nothing seems to work: index, xpath, anything. Even though it might be a little confusing to figure out why would you want to scroll in columns that are in the same horizontal level, it would make more sense if you imagine them as col-md-12, for example, so it would scroll down on mobile or something like that.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Do not try to do it in a single line. Put some logic into it. Check if there is a next column, and if not go up to the row and check if it has a next row with a column

$(".btn").click(e => {
  let element = $(e.target);
  let nextCol = element.closest('.col-md-6').next();
  if (!nextCol.length) {
    nextCol = element.closest('.row').next().find('.col-md-6').first();
  }
  if (nextCol.length) {
    $([document.documentElement, document.body]).animate({
      scrollTop: nextCol.offset().top
    }, 2000);
  }
});
/* this is just to show case the animation in the demo */
.col-md-6 {
  padding: 5em;
  margin: 5em 1em;
  border: 2px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="row">
  <div class="col-md-6">
    <a class="btn">Button 1</a>
  </div>
  <div class="col-md-6">
    <a class="btn">Button 2</a>
  </div>
</div>
<div class="row">
  <div class="col-md-6">
    <a class="btn">Button 3</a>
  </div>
  <div class="col-md-6">
    <a class="btn">Button 4</a>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda