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

154
Vistas
Changing JS code to work with other elements - HTML/CSS/JS

If I have the following JS code:

$(document).ready(function() {
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function() {

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

This is the HTML:

<a href="#about">
  <div class="containerScroll">
    <div class="first-scroll"></div>
    <div class="second-scroll"></div>
  </div>
</a>

Right now the scrolling effect is working for all links with the tag a; however, can I change the $("a").on('click', function(event) { to something like $("scroll").on('click', function(event) {? Now, I'm hoping it will work for all the tags with scroll so now my HTML would look like:

<scroll href="#about">
  <div class="containerScroll">
    <div class="first-scroll"></div>
    <div class="second-scroll"></div>
  </div>
</scroll>

Would this work? Essentially, I want to add another tag instead of a because I do not want the scrolling effect to take place on all the links and only the ones I choose. This is why I tried changing the tag to scroll but it still did not work? Am I supposed to define a scroll css property for it to work?

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

0

Using jQuery for smooth scrolling is pointless nowadays

There are two methods (#2 is better IMO)

const element = document.querySelector('#something')
// element is the target you're trying to scroll to
  1. JS Method- Works just for the element you've selected
element.scrollIntoView({ behavior: "smooth" })
  1. CSS Method
/* in CSS */
html {
    scroll-behavior: smooth;
}
//in JS
element.scrollIntoView()
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