/post

Back to top button

Posted by adrengski on Friday, September 30th, 2011
Tagged as: easy tutorials


When your infinite scrolling or Auto pager is enabled, be sure to put a cool back to top button because scrolling all the way up through your God-knows-how-many posts could be a bit frustrating for your readers/visitors. Making a quick search on Google would lead you to so much result that you won’t be able to decide which to choose.

Well, just to add up with the confusion, here’s a tutorial to put a nice Back-to-top button to your blog like the one in this blog.

Proceed to your blog’s Customize page then click on the Edit HTML button. After that, paste these codes just above the </style> tag of your HTML codes:

#scrollToTop {
 cursor: pointer;
 position: fixed;
 background-color: #EEE;
 z-index: 300;
 display: block;
 color: #222;
 text-align: center;
 font-size: 13px;
 border: 1px solid #BBB;
 box-shadow: 0 0 0 1px #fff inset, 0 1px 4px rgba(0,0,0,.2);
 -moz-box-shadow: 0 0 0 1px #fff inset, 0 1px 4px rgba(0,0,0,.2); 
 -webkit-box-shadow: 0 0 0 1px #fff inset, 0 1px 4px rgba(0,0,0,.2);
 right: 15px;
 bottom: -6px;
 width: 70px;
 padding: 10px;
 -moz-transition: bottom 250ms ease-in-out;
 -webkit-transition: bottom 250ms ease-in-out;
 border-radius: 5px 5px 0 0;
 text-shadow: 0 1px white;
}

#scrollToTop.offscreen {
 bottom: -100px;
 -moz-transition-duration: 250ms;
 -webkit-transition-duration: 250ms;
}

#scrollToTop:hover {
 background-color: #f4f4f4;
 color: #000;
}

#scrollToTop:active {
 padding: 10px 10px 11px;
}

That will be your Back-to-top button’s CSS/style. That would make it look like the one in this blog. If you know bits of CSS, you can edit these to suit your liking.

Next, after that, paste these codes just after the <head> section of your codes:

<script type="text/javascript">

$(document).ready(function(){

    // hide #back-top first
	$("#scrollToTop").addClass("offscreen");
	
	// fade in #back-top
	$(function () {
		$(window).scroll(function () {
			if ($(this).scrollTop() > 1000) {
				$('#scrollToTop').removeClass("offscreen");
			} else {
				$('#scrollToTop').addClass("offscreen");
			}
		});

		// scroll body to 0px on click
		$('a#scrollToTop').click(function () {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	});

});

</script>

This jQuery codes will be the one doing the hiding and showing up of the Back-to-top button. See that bold, underlined text(1000)? You can change that to any number you like to change when the button will appear. 1000 means the button will appear if you’ve reached 1000px from the top. If you are somewhere less than 1000px, the button will hide.

Lastly, add this link just before the </body> tag:

<a href="javascript:;" id="scrollToTop">Scroll to top</a>

That’s it! After that, click on the Update Preview button, then once the Save Button appears, click on it, too. Close the Customize Page and check it in your blog!


1 note | Comments

Comment

blog comments powered by Disqus

Notes

  1. tutobx posted this
/ask

Questions! Questions!


Before asking a question, make sure to check the FAQs page or the Answers page first incase it's already answered there. If it isn't then ask away!

All questions/TAs, anon or not, will be published on the Answers page so check it if it's already there. The post will be tagged under your URL , too, so tracking your URLs would be a nice way to notify you.

Also note that I may not be able to answer questions ASAP since unfortunately, I also have this evil stuff called "Real life" and it requires me to do things like "Working" and "Socializing". Rest assured though, that I will be answering all questions whenever I go online and if there's any.

DONATE! On a related note, You could also increase the chance of getting answered immediately by Donating! You see, if you donate, that would mean more moolah. If there's moolah around, I don't have to work that much. And if I don't have to work that much, that would mean more time in answering your queries! I mean, that's the only thing that's keeping me away from here anyway. So yeah, a buck wouldn't hurt, right?


Scroll to top