Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda