top" scroll to the top of the webpage when clicked using jQuery. Note as per the comment by Jono you can use the id of an element to scroll to. Have a read through this post then a look at his comment.
Note the "return false;" line. This is required otherwise the default action will be done and the instant scroll will happen instead of the nice animated scrolling effect.
The position the document is scrolled to can be different by changing the scrollTop value, the speed by changing the ‘slow’ value and what appears in the href by chaging the
href value.
Note that an anchor should still be present in the page for browsers that do not have Javascript on or support jQuery.
We have added the dummy images so our HTML page becomes a little longer for scrolling to properly work. Also, we added some basic styling for our ScrollToTop button in style.css.
Now actual logic is written in our “app.js” file. Let’s explain it:
Feel free to check this whole code. You can download it from our Github Repo directly with this link:
Want more?
What if you want to activate your scroll-to-top functionality after a certain distance is scrolled down on your webpage? Then just make these small modifications to your jQuery code:
Using the jQuery scroll() function we first determine if enough page is scrolled. Then only we start showing the scrolltotop button to website visitors. And once we reach the top of the page, the button disappears again.
$(window).scroll(function () {
if ($(window).scrollTop() >= 600) {
Using javascript scrolltotop is ideal in a situation where you cannot use the jQuery library. There are multiple ways to achieve this. Here are the 3 most used methods to build javascript scrolltotop:
With window.onscroll()
This is the easiest method when using only javascript in your project.
With document.documentElement.scrollTop()
If for any reason, our previous Javascript scrolltotop method didn’t work. Feel free to try this method. Check out the below CodePen where we give you a live example:
With document.documentElement.scrollIntoView()
Lastly, try this new javascript scrolltotop method using the document object modal. We explain it in the below CodePen example:
Note: Adding “scroll-behavior: smooth” is a must in your CSS file. Without this code, your scrolling animation will be instantaneous and will feel jerky.
If you are looking to bling up your website with Page Scroll animations, check out our post on 6 methods to implement page scroll animation.
Check out our code at Github Repo directly:
Scroll to Top with No Javascript(using HTML fragments)
HTML fragments allow scrolling to any section of a webpage linked with the “id” and “
By the way, if you are trying to learn how to build an HTML form with PHPmailer check out this guide.
You can check this code in our Github repo:
Conclusion
We highly recommend using the jQuery method because it’s short and less verbose. Use Javascript Scroll To Top functionality only when your project doesn’t need jQuery at all.
We hope you enjoyed our article on how to build a js scroll to top button for your website. With this knowledge, we know that you can implement scrolling to the top of your website to make it easier for your customers to find important information.
So what are you waiting for? Check out our other post where we build an Image Format Converter. Also if you are a fan of Isekai Anime, check our article on 15 Isekai Anime with OP mc.
Thanks so much for reading. If you have any doubts, comment and ask us. We always try to give prompt replies. Keep coding! Ta-da!
How do I scrollTo the top of my screen?
Alt+Ctrl+PageUp and Alt+Ctrl+PageDown are the built-in keyboard shortcuts to move the insertion point to the top and bottom of the window.
How do I make my HTML page scrollTo the top?
A standard HTML tag will suffice... +1 This is good if you need to navigate to specific element rather just to the top. Use 'Top' to jump to the top of the page.
How do you auto scrollTo the top of the page in JavaScript?
To make the page jump on the top instantly can be done with the native JavaScript method, namely — window. scrollTo(x-coord, y-coord) . This property of the window interface scrolls to a particular set of coordinates in the document.
How do you jump to the top of the page?
Please use Ctrl + Home command from your keyboard to scroll to the top of the current page.