Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 129 characters in body
Source Link
marcXandre
  • 2.5k
  • 2
  • 16
  • 21

Change animateToTop to this and add scroll-behavior: smooth;. It should do the work:

function animateToTop(e) { window.scrollTo(0, 0); } window.addEventListener('scroll', (e) => { var scrollTopBtn = document.getElementsByClassName('scrolimg')[0]; if (window.scrollY >= 100) { scrollTopBtn.style.visibility = 'visible'; } else { scrollTopBtn.style.visibility = 'hidden'; } }); 
html { scroll-behavior: smooth; } 

You can also use:

document.body.scrollTop = 0; document.documentElement.scrollTop = 0; 

But there's no more animation

Change animateToTop to this and add scroll-behavior: smooth;. It should do the work:

function animateToTop(e) { window.scrollTo(0, 0); } window.addEventListener('scroll', (e) => { var scrollTopBtn = document.getElementsByClassName('scrolimg')[0]; if (window.scrollY >= 100) { scrollTopBtn.style.visibility = 'visible'; } else { scrollTopBtn.style.visibility = 'hidden'; } }); 
html { scroll-behavior: smooth; } 

Change animateToTop to this and add scroll-behavior: smooth;. It should do the work:

function animateToTop(e) { window.scrollTo(0, 0); } window.addEventListener('scroll', (e) => { var scrollTopBtn = document.getElementsByClassName('scrolimg')[0]; if (window.scrollY >= 100) { scrollTopBtn.style.visibility = 'visible'; } else { scrollTopBtn.style.visibility = 'hidden'; } }); 
html { scroll-behavior: smooth; } 

You can also use:

document.body.scrollTop = 0; document.documentElement.scrollTop = 0; 

But there's no more animation

Source Link
marcXandre
  • 2.5k
  • 2
  • 16
  • 21

Change animateToTop to this and add scroll-behavior: smooth;. It should do the work:

function animateToTop(e) { window.scrollTo(0, 0); } window.addEventListener('scroll', (e) => { var scrollTopBtn = document.getElementsByClassName('scrolimg')[0]; if (window.scrollY >= 100) { scrollTopBtn.style.visibility = 'visible'; } else { scrollTopBtn.style.visibility = 'hidden'; } }); 
html { scroll-behavior: smooth; }