Paulund

How To Create An Animated Scroll To Top Button With jQuery

Scroll To The Top

Creating a good user experience on your website is very important to keep people on the page. The best way to create a good user experience is to make it easy for people to use, if your site is difficult to use it annoys people and they will move on.

For websites that have a lot of information on the page you will scroll far down the page to consume the information. Websites like Google+ who have infinite page scroll can be very annoying if you want to go back to the top of the page to click on a link there. You can either refresh the page or move the scroll bar all the way back to the top.

Websites with initiate scroll need something to make it easy to return to the top. This is done with a button which will scroll them back to the top.

In this tutorial you will learn how you can create an animated scroll to top button with jQuery.

Demo

The Button

First lets create the button.

<a href="#" class="scrollToTop">Scroll To Top</a>

Now we can style the button with the following CSS.

.scrollToTop{
	width:100px;
	height:130px;
	padding:10px;
	text-align:center;
	background: whiteSmoke;
	font-weight: bold;
	color: #444;
	text-decoration: none;
	position:fixed;
	top:75px;
	right:40px;
	display:none;
	background: url('arrow_up.png') no-repeat 0px 20px;
}
.scrollToTop:hover{
	text-decoration:none;
}

This will position the scroll to top button at the top right of the page which we have fixed to stay in this position.

As you can see from the CSS we have set the display to be none so it will be hidden in the browser, you will see why we do this in the jQuery code.

jQuery Part

Below is the jQuery part of this functionality, we will add a action on the scroll event to make the button appear when not at the top of the window and then make the button disappear when we are at the top of the page.

We will then need to create a click event on the button to scroll to the top of the window.

Copy and paste the following in the Javascript file to add the Javascript functionality.

$(document).ready(function(){

	//Check to see if the window is top if not then display button
	$(window).scroll(function(){
		if ($(this).scrollTop() > 100) {
			$('.scrollToTop').fadeIn();
		} else {
			$('.scrollToTop').fadeOut();
		}
	});

	//Click event to scroll to top
	$('.scrollToTop').click(function(){
		$('html, body').animate({scrollTop : 0},800);
		return false;
	});

});

This uses the scroll function which is ran when the visitor scrolls the window. We can then work out the position of the window by using the scrollTop function, if the position is move than 100 then we want to display the button.

We display the button by using the fadeIn function to add the first bit of animation to the screen. If the scrollTop is less than 100 then we know that the window is near the top so we don’t need to display the button.

Now we can add a click event to the scroll to top button. On the click action of this button we want to scroll the page back to the top n animation by using the animate function. To scroll back to the top of the window we need to change scrollTop property to 0.

That’s all you need to recreate this very useful feature, view the demo to see what it will create.

Demo

Getting Good With Javascript

Writen By Paul

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5.

Website: http://www.paulund.co.uk

Feedback

Anything to add?

  • http://www.paulund.co.uk Paul

    It’s always a nice little feature to have on a site and so easy to do with jQuery.

  • http://www.wirelesswebstudio.com/ Rizwan Sayyed

    Great Feature :)

    • http://www.paulund.co.uk Paul

      Thanks Rizwan

  • http://www.logoguru.co.uk/ Sam @ Logo Design

    Thanks for the Tutorial! it will be useful to me as Graphic Design learner! thanks for posting here!

    • http://www.paulund.co.uk Paul

      Thanks Sam glad its helping.

  • http://web2feeds.com Web2 feeds

    Useful tut, thanks. I shared your post. :)

    • http://www.paulund.co.uk Paul

      Thanks web

  • http://www.logoblog.org/ Business Logo Design

    I just check on my blog!! this animated scroll works very great :) thank you for sharing.

    • http://www.paulund.co.uk Paul

      Nice one good work. Glad it helps.

      Let me know if you need any other tutorials.

  • http://www.facebook.com/people/Ben-Michael-Levy/1026407675 Ben Michael Levy

    How can you add a fade to the middle sections of your page so that when your not at the top or all the way at the bottom it fades out but you can hover over the section to see it again

    • http://www.paulund.co.uk/ Paul

      Hello Ben

      I’m not too sure what you mean.

      By using the if statement

      if ($(this).scrollTop() > 100) {
      $(‘.scrollToTop’).fadeIn();
      } else {
      $(‘.scrollToTop’).fadeOut();
      }

      It will only fade in after you scroll more than 100 pixels. To change where is fades in just change the 100 to something else.

      Do you want it to fade in when you hover over a section?

      So if you had a section with a class of content then you can add a hover event to the section.

      $(‘.content’).hover(function(){
      $(‘.scrollToTop’).fadeIn();
      });

      Is that what you meant?

      Hope this helps

      Thanks
      Paul

      • http://www.facebook.com/people/Ben-Michael-Levy/1026407675 Ben Michael Levy

        What I meant is that currently the arrow stays on the right side and only fades away when all the way at the top. I would like it to fade in and then where ever the user stops fade away after a set time until they start to scroll again except stay visible if they are all the way at the bottom of the page. Thanks!

        • http://www.paulund.co.uk/ Paul

          Ok Ben I think I understand it now, but I don’t see why you would want it to fade away if the user scrolls down you always want an option to go directly back to the top (if thats where you main navigation is).

          From what I know I don’t think there is an event you can get at when the browser stops scrolling.

          This will tell you all you need to know about the scroll event http://api.jquery.com/scroll/.

          If there is no event for this then the only other way I can think of doing this is to run a function every 10 seconds to check the position of the scroll bar using scrollTop and then record the position it returns, on the next call of the function compare with the last position if the same then you can hide the scroll to top button.

  • Anonymous

    Useful.

  • http://www.paulund.co.uk/ Paul

    Hello

    Glad you like the tutorial.

    The 800 is for the duration of the animation. This is saying that from where ever we are on the page take 800ms to scroll to the top of the page, so if you are at the bottom of the page you will see it move quicker than if you were near the top of the page.

    Have a look at the jQuery documentation if you want more information about the animation function.

    http://api.jquery.com/animate/

  • http://www.paulund.co.uk/ Paul

    Hello

    Was the jQuery being included correctly?

    Thanks

    • PaffyJones

      Thank you for replying. I have included it like this:

      ** JQUERY CODE **

      But it won’t work. display:none; => the arrow vanishes and it won’t come back. :

      • http://www.paulund.co.uk/ Paul

        Do you have a demo page I can see?

  • Ritter Cox

    This is helpful, thanks.

    • http://www.paulund.co.uk/ Paul

      Your welcome Ritter

    • http://www.paulund.co.uk/ Paul

      Your welcome Ritter