Creating Scroll To Top Button Using jQuery

So, you do not have the scroll to top button in your site and desperately want it to be added in your site for the easy navigation of the user and came landed here as the help. Then, you have came in the right place. Here, we will describe it about how it need to be added in your site.

First of all, what you need to do is, you have to find out the unique id of the specific place of your site in which you want the scrolling to be added in your site. For this, you can look at the specific file and then find out the unique id of that place. For eg: here below, is the html code having the unique id for that specific div in our site:

Now, here the main thing which is needed is that, you need to add the unique id for the specific div, as in the above example, our unique id is #masthead. If you add this same id in other place on your site, then, scrolling option will not work properly, so, special care should be taken while creating it in your site. Also, this codes probably resides in the top of your site, ie, header area.

Now, if you have done the above mentioned work in your site, now, you need to apply in other place, for eg: in the footer area, the below html code.

Here, in the above code, what is done is, we have added the link to the id of masthead. By this way, clicking on this, since, this id already resides in your site, if done above provided process, then, it should directly navigate to that place, with no animation effect at all. So, this might be little annoying for you as you could not see any scrolling effect and see the direct jumping effect in your site.

So, now you want to add the scrolling effect for that scroll to top button in your site. For this, you need to write the custom js code like the one below:

What we have done above is, first of all, we have added the CSS via jQuey script to hide the scroll to top button, with the code below:

Again, the code below is used to display the scroll to top button in your site, when the browser height is greater than 1000px when viewing your site. But, if you need the button to appear very quickly, you can change the value of 1000 as you require for your site.

Now, the final script code to make the animation work for scroll to top button comes up, which is below:

Here, what we have done is, we have added the clicking event for the button we have created already in the above steps to get animated as scrolling. Here, the timing of the scrolling is controlled via the code 1000. So, if you want to change that value according to your wish and site requirement, then, you can do it.

And, lastly, if you want to add some styling to that button for your site, then, you can paste the below CSS code in your style.css file of the theme or in anywhere you want, but, that file need to be connected in your site:

Here, below is the screenshot on this when all of the above mentioned steps are done properly in your site:

scroll-to-top-button

Since, you now have the knowledge on creating the scroll to top button in your site, why not apply it and then create the smooth transition effect for the users of your site.

, , , , , ,

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply