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

168
Views
JS desplazándose hacia arriba después de window.scroll

Hola, necesito que cuando visite mi página web y # esté presente en la URL, vaya a esa sección. Entonces, lo que hice fue agregar un script JS

 document.addEventListener('DOMContentLoaded', () => { // setupScrolling(); setupTitle(); if (window.location.hash) { const element = document.getElementById(window.location.hash); if (element) { const offset = element.offsetTop; const a = offset - (document.querySelector('header')?.offsetHeight); window.scroll({ top: offset, behavior: 'smooth' }); return false; } } });

Este es el script que agregué a mi public/index.html

Pero el comportamiento no es correcto. Una vez que la página se desplaza hacia abajo hasta la sección, volverá automáticamente a la parte superior del sitio y no sé por qué.

Alguien me puede ayudar y decirme que debo hacer?

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

0

Realmente no necesitas javascript para hacer esto. Solo puede usar HTML.
Simplemente agregue una identificación a los elementos a los que desea permitir que los usuarios salten.
Luego, cuando la identificación de un elemento está presente en la URL de su página web como un hash, la página se desplazará directamente hacia él cuando se cargue la página.

Por ejemplo, digamos que tengo un elemento con el id 'sección inferior'

 <span id="down-section">Down Section in the page</span>

El acceso al enlace https://your-domaine.com#down-section saltará directamente a " Sección abajo en la página ".

Añade un comportamiento suave.

Es posible que desee agregar un comportamiento suave en este caso particular. Simplemente agregue un código como este a su script javascript:

 if (!!document.location.hash){ document.getElementsByTagName('html')[0].style.scrollBehavior = 'smooth'; }
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!