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

163
Views
Cambiar el código JS para que funcione con otros elementos - HTML/CSS/JS

Si tengo el siguiente código JS:

 $(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 }); });

Este es el HTML:

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

En este momento, el efecto de desplazamiento funciona para todos los enlaces con la etiqueta a ; sin embargo, ¿puedo cambiar $("a").on('click', function(event) { a algo como $("scroll").on('click', function(event) { ? Ahora, yo' Espero que funcione para todas las etiquetas con scroll , así que ahora mi HTML se vería así:

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

¿Funcionaría esto? Esencialmente, quiero agregar otra etiqueta en lugar de a porque no quiero que el efecto de desplazamiento tenga lugar en todos los enlaces y solo en los que elija. ¿Es por eso que intenté cambiar la etiqueta para scroll pero aún no funcionó? ¿Se supone que debo definir una propiedad css de scroll para que funcione?

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

0

Usar jQuery para un desplazamiento suave no tiene sentido hoy en día

Hay dos métodos (#2 es mejor IMO)

 const element = document.querySelector('#something') // element is the target you're trying to scroll to
  1. Método JS: funciona solo para el elemento que ha seleccionado
 element.scrollIntoView({ behavior: "smooth" })
  1. Método CSS
 /* in CSS */ html { scroll-behavior: smooth; }
 //in JS element.scrollIntoView()
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!