Este problema me ha preocupado durante mucho tiempo: quiero que la página llegue a la parte superior sin desplazarse.
Intenté esto, pero no funcionó:
window.scrollTo({ top:0, behavior: 'auto' });necesitas tanto x-coord como y-coord
Prueba esto
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });si su objetivo es evitar el desplazamiento real (la "animación"), use behavior: 'instant' .
const button = document.getElementById('instant-top-button') button.addEventListener('click', () => window.scrollTo({ top: 0, behavior: 'instant', })) <p>scroll down ⭳</p> <p> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> </p> <button id="instant-top-button">⭱ to top, instantly!</button>por favor, intente una solución simple. Simplemente reemplace su código por esto aquí:
window.scroll(0, 0);Saludos, marcelo