jQuery topLink Plugin

Written by David Walsh on Monday, March 2, 2009


Last week I released a snippet of code for MooTools that allowed you to fade in and out a “to the top” link on any page. Here’s how to implement that functionality using jQuery.

The XHTML

<a href="#top" id="top-link">Top of Page</a>

A simple link.

The CSS

#top-link	{ display:none; position:fixed; right:5px; bottom:5px; color:green; font-weight:bold; text-decoration:none; border:1px solid green; background:Lightgreen; padding:10px; }

A little CSS for position and style.

The jQuery Javascript

//plugin
jQuery.fn.topLink = function(settings) {
	settings = jQuery.extend({
		min: 1,
		fadeSpeed: 200
	}, settings);
	return this.each(function() {
		//listen for scroll
		var el = $(this);
		el.hide(); //in case the user forgot
		$(window).scroll(function() {
			if($(window).scrollTop() >= settings.min)
			{
				el.fadeIn(settings.fadeSpeed);
			}
			else
			{
				el.fadeOut(settings.fadeSpeed);
			}
		});
	});
};

//usage w/ smoothscroll
$(document).ready(function() {
	//set the link
	$('#top-link').topLink({
		min: 400,
		fadeSpeed: 500
	});
	//smoothscroll
	$('#top-link').click(function(e) {
		e.preventDefault();
		$.scrollTo(0,300);
	});
});

You’ll see that I’ve added jQuery’s ScrollTo plugin to add some smoothness to the anchor.

Please note that this version doesn’t work with Internet Explorer due to IE’s lack of CSS “position:fixed” support. Here is a shotty attempt at IE support:

//plugin
jQuery.fn.topLink = function(settings) {
		settings = jQuery.extend({
			min: 1,
			fadeSpeed: 200,
			ieOffset: 50
		}, settings);
		return this.each(function() {
			//listen for scroll
			var el = $(this);
			el.css('display','none'); //in case the user forgot
			$(window).scroll(function() {
				//stupid IE hack
				if(!jQuery.support.hrefNormalized) {
					el.css({
						'position': 'absolute',
						'top': $(window).scrollTop() + $(window).height() - settings.ieOffset
					});
				}
				if($(window).scrollTop() >= settings.min)
				{
					el.fadeIn(settings.fadeSpeed);
				}
				else
				{
					el.fadeOut(settings.fadeSpeed);
				}
			});
		});
	};

Know of a better way to incorporate IE support? Share it!


Follow via RSS Epic Discussion

Commenter Avatar March 02 / #

Hmm… I thought you can do el.hide() instead of el.css(‘display’,'none’)? :P

Commenter Avatar March 02 / #

Wow, nice plugin, I think I might incorporate this into a website in the not so long future. And good luck with the submission to the JQuery website :)

David Walsh March 02 / #

@Lim Chee Aun: Good call! Updated.

Commenter Avatar March 02 / #
RaduM says:

If you use something like this:

body { overflow: hidden; }
div.content {
height: 100%;
overflow: auto;
}

shouldn’t work? I will have to try it :), nice article anyway

Commenter Avatar March 02 / #
Ben says:

It shows up fine but when I click it, it doesn’t scroll. Do you know why this would be?

Commenter Avatar March 03 / #

Hi David! I translate to Turkish and publish to my site. I hope you like it. Here is link -> http://www.kadirgunay.com/jquery-top-link-jquery-yukari-cik-baglantisi.html
And i added your link bottom of the article :)

Thank you, and see you

Kadir,

Commenter Avatar March 03 / #
adel says:

Nice job David !!

Commenter Avatar March 03 / #
E11World says:

Hi All,
Nice plugin and really useful. I’ve got a fix for IE7 (need to test ie6 if you care about it)

Basically I added the last value in the full CSS below *height: 1.5%
For those who don’t know the hack. The * makes it an IE7 value only (you could use _ for ie6) and FFox ignores it. I don’t think its W3C standards though but it does the job.

#top-link {
display: none;
position:fixed;
right: 5px;
bottom: 5px;
color: #eeefea;
font-weight: bold;
text-decoration: none;
border: 2px solid #1d2835;
background: #222b3a;
padding: 6px 10px;
*height: 1.5%
}

As for t the problem of the full script not working. I had a hard time with it too. Copying and pasting the code doesn’t work for some reason so I downloaded the demo example and striped out what I needed and it worked. I hope this was usefull to someone.

Commenter Avatar March 03 / #
Faisal Alim says:

i like it

Commenter Avatar March 04 / #
Akshit says:

nice one..surely goin 2 try my hands on it!!

Commenter Avatar March 07 / #

Awesome script David! Implemented it on opensourcereleasefeed.com. Thanks!

Commenter Avatar March 18 / #
Janko says:

I love the solution! Good work!

Commenter Avatar April 19 / #
Javis says:

wow nice , i m going to use it on my blog

Commenter Avatar June 01 / #
ganesh says:

I noticed its not working with IE…is there a way to solve it i need it…!

David Walsh June 01 / #

Works for me!

Commenter Avatar August 06 / #

Has anyone yet mentioned today that IE sucks?

Commenter Avatar August 06 / #

David you do not cease to surprise :)

Commenter Avatar September 02 / #
Matt says:

A really amazing idea David, thanks! I made a similar plugin inspired by yours,
nice work!

Commenter Avatar September 03 / #
Pawan says:

@Matt,
I saw your plugin, looks very good. Can you tell how to use it to scroll to any specific control, rather than to the top of the page.

In my case, I want to scroll to say my DIV named “header” : <div id=”header”> </div>

Thanks,
Pawan

Commenter Avatar September 15 / #
Aero says:

Important thing for a longer post. Thanks. I will publish it in my Bangla blog Bangla Hacks.

Commenter Avatar October 03 / #
DanC says:

Hey David,

Just wanted to add that this is a fantastic script, many thanks for producing it. I am implementing it slightly differently, without the fixed positioning, so no IE problems so far!

Many thanks again,

DanC

Commenter Avatar November 23 / #

nice plugin..
thanks.. :)

Commenter Avatar December 13 / #
Tm0 says:

I would like to thank you so much! I have been trying with JS to work this out, with no avail. This helps so much. I even bookmarked your site! (Which for me is a rarity) I will also link you back on my index.

Thanks again,

Tm0.

Commenter Avatar December 19 / #

where i must place javascript code for blogger

example: between or ???

Commenter Avatar January 03 / #

Hey David,

Happy New Year to you! Just wanted to drop you a quick note about topLink. I have been using it often in my web dev work and really enjoy the usability it adds to sites.

I actually think all sites should implement some version of back to top and as such I converted your topLink plugin into a Chrome extension ;) https://chrome.google.com/extensions/detail/afpmpmikeidpfnfmnmlpecnoipnnnfmn

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.