Scroll to top js

If you love to create like a scroll to top js i have recommended some code here for you.

// first we have to create a variable then select the classlist or id you have in your html using the querySelector
const scrood = document.querySelector('.scrollTop');
//secondly, you have to apply some listener to the window then create a function
window.onscroll() = function () {
// thirdly, create another function with the previous function name 
function scrollFunction() {
// then create a conditional statement 
if(document.body.scrollTop > 20 || document.documentElement.scrollTop > 20 ) {
scrood .style.display = '' block'';
} else { = ''none'';
//lastly, we are going to apply an event listener to the the variable name called scrood and the event we are listening for is click event and then we create a function inside it.
scrood.addEventListener('click', () => {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}) ;

Don’t forget to close your work with a semi colon ;

1 Like

But semi colon is optional is JS.

I don’t add semi colon to my work, I leave it for my formatter(prettier) to handle.

That’s nice but i think you need to include so that your won’t look complicated