DEV Community

Cover image for Making a 'Back to Top' button without Javascript

Making a 'Back to Top' button without Javascript

Jack on October 11, 2020

The 'back to top' button Here is my tutorial on making a 'back to top' button without JavaScript. Please also refer to these very helpfu...
Collapse
 
viv profile image
Vivek Siva

JS devs be like " where are my animationsssss"

Collapse
 
capsule profile image
Thibaut Allender

You mean html {scroll-behavior: smooth;}?

Collapse
 
jackherizsmith profile image
Jack

Haha! What kind of animations are you thinking about?

Collapse
 
viv profile image
Vivek Siva

all those gimmicy animations, like lets a smooth scrolling to top (like a river :D) or an top arrow that changes color depending on how bottom we are on page etc!

Thread Thread
 
jackherizsmith profile image
Jack

Love the idea of colour changing when scrolling! Or perhaps fading in to full opacity as you get to the bottom...

Collapse
 
rtivital profile image
Vitaly Rtishchev

well, it kind of works but messes up with browser history, user will have to click back button twice which is a bad UX, so just, please, stick to the js solution instead of link

Collapse
 
jackherizsmith profile image
Jack

Hey Vitaly! Thanks for your comment. What browser are you using? href="#" just navigates to the top of page, it doesn't add to the history in the browser which I should have mentioned in the article, I'll update that now. If you have any other reasons to suggest using JS on top of CSS I'd love to hear them.

Collapse
 
capsule profile image
Thibaut Allender

The question is more, what browser doesn't add it to the history? Both the latest Firefox and Chrome add # to the history.

Collapse
 
futureistaken profile image
R Z

Anyway, it's a good idea if you don't want to use onScroll event. You just can add a little bit JS to replace the link with a button.

Collapse
 
rtivital profile image
Vitaly Rtishchev

Yep, that's what I've meant, when replacing js with css, do not forget about the UX

Collapse
 
madmax124 profile image
Syed Saad

Noice

Collapse
 
contradicthelaw profile image
Minh Nguyen (he/him)

I like this! A couple of the websites I help manage has this effect, but I wasn't sure how it was achieved.

Collapse
 
anders profile image
Anders

Very elegant : )

Collapse
 
jackherizsmith profile image
Jack

Very kind thank you!

Collapse
 
whjeon profile image
WooHyung Jeon

Amazing! learned alot!