O'Reilly

“Top” Watermark Using MooTools

By on  

Whenever you have a long page worth of content, you generally want to add a "top" anchor link at the bottom of the page so that your user doesn't have to scroll forever to get to the top. The only problem with this method is that I may want to get to the top of the page from somewhere in the middle. Using MooTools, we can create a "top" link as a watermark that follows the users down the page.

The XHTML

<a href="#top" id="gototop" class="no-click no-print">Top of Page</a>

Place this anywhere inside the page that you would like.

The CSS

#gototop { display:none; position:fixed; right:5px; bottom:5px; }

This code positions the element in a fixed position so it always stays in the same spot.

The MooTools JavaScript


/* smoothscroll - scroll smoothly to the top*/
new SmoothScroll({duration:500});

/* go to top after 300 pixels down */
var gototop = $('gototop');
gototop.set('opacity','0').setStyle('display','block');
window.addEvent('scroll',function(e) {
	gototop.fade((window.getScroll().y > 300) ? 'in' : 'out')
});

The code is surprisingly short. Of course we use SmoothScroll to make the transition smoothe. You may adjust the pixel trigger amount however you'd like. Unfortunately the above code doesn't work in IE6.

The IE Fix

/* go to top */
var gototop = $('gototop');
gototop.set('opacity','0').setStyle('display','block');
window.addEvent('scroll',function(e) {
	if(Browser.Engine.trident4) {
		gototop.setStyles({
			'position': 'absolute',
			'bottom': window.getPosition().y + 10,
			'width': 100
		});
	}
	gototop.fade((window.getScroll().y > 300) ? 'in' : 'out')
});

I like this method so much that I use it on my website.

Shouldn't this be a plugin?

Don't worry -- ScrollSpy is coming soon enough. :)

Track.js Error Reporting

Recent Features

  • 6 Things You Didn&#8217;t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving,...

  • CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • Create a 2 Column Layout with Flexbox

    Flexbox was supposed to be the pot of gold at the long, long rainbow of insufficient CSS layout techniques.  And the only disappointment I've experienced with flexbox is that browser vendors took so long to implement it.  I can't also claim to have pushed flexbox's limits, but...

  • Create a 3D Animating Sidebar

    Mozilla's Christian Heilmann is an evangelist that knows how to walk the walk as well as talk the talk.  You'll often see him creating sweet demos on his blog or the awesome Mozilla Hacks blog.  One of my favorite pieces...

Discussion

  1. Alelo

    hi, realy nice but the ie fix doesnt work right in IE8.0.6001(the newest Ie8 version) its comes but the “absolute” seems to be wrong, shouldnt it be position”fixed”?

  2. wonderfull… i m making an agenda in mootools (moogenda ;) ) where i have a diary for week or day that is very long… i think i’ll add this script tonight asap. thank you for the suggestion.

    Nunzio Fiore

  3. Santiago

    Very interesting effect indeed!
    It didn’t work for me in IE 7 though, it stay in the same place, doesn’t get the new height of the window, maybe because you forgot to add the “px” subfix to the bottom and width attributes in the css?

    I don’t have time right now to test it in my own, but I’ll probably do it when free ;)

    @Alelo:
    IE, at least until the 7 version, didn’t support at all position:fixed;, so in IE we’ll need a fix, in other words, need to change it’s position with javascript according to the window position.

    Cheers!

  4. anonymous

    Nice, since I just started to learn Mootools.
    But couldn’t we achieve the same effect with some css and position-fixed or background-attachment?
    What do u think?

  5. Guillermo Rauch

    Another trick would be checking that Window getScrollHeight() is larger than getHeight() to fade in the element.
    I also recommend using Element.Pin from the upcoming mootools-more

    http://github.com/anutron/mootools-more/blob/50f23f10f0de3bb8e851486e43028c9d973ce97e/Source/Element/Element.Pin.js

  6. seems that you have been peeking at my blog ;)
    http://blog.gonchuki.com/archives/usability-101-adding-a-global-back-to-top-to-your-site/

    You should take a look at the study we made back in 2007 about how much scroll is needed before we can confidently show the “back to top” element.

    btw, use Browser.Engine.trident4 if you want to isolate ie6, or use a CSS2 selector like we did back then to completely ignore ie6.

  7. IE position is not fixed

  8. Updated guys. The “trident4″ update was the golden gem for IE7.

  9. RedTreys

    Very nice.
    Any chance that you (or anyone else who would like to jump in) could offer its equivalent using jquery?

  10. I’ll add that to the to-do list RedTreys. Thank you!

  11. Rodney

    Thanks David, just the info I needed.
    Thanks again.

  12. How do i use this code with wordpress?

  13. Nils

    How to change the code making it work with mootools 1.11?

  14. Emil

    Like Nils, I’d like to know how the code looks like for mootools 1.11

  15. What’s to do that it work’s with mootools-1.2.3.1-more.js
    Updating this

    var go = document.id('gototop');
    

    doesn’t work.
    any idea
    thx

  16. Ana

    Yeah! Very nice post! Thanks a lot for this…

    Unfortunately someone stole your WHOLE post:
    http://adilkhan116.posterous.com/top-watermark-using-mootools

    Keep going,
    Ana

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Chris Coyierâs Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it’s damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...

  • Copy a Directory from Command Line

    Copying a directory for the sake of backup is something I do often, especially when I'm trying to figure out why something isn't working when I use an external library.  I'll copy the directory structure as a backup, mess around with the original source until I find a solution,...

  • Hotjar &#8211; All-in-one Analytics and Feedback

    Website analytics are a massive business -- the more data you can collect with regard to your users' behaviors on your site, the more you can increase and maximise conversion...and increased conversion is always good.  Sometimes increase conversion means more money, improved user experience, viewer retention,...

  • Crafting a 3D React Carousel

    There is something in me that is amazed but beautiful 3D interfaces. And it doesn’t matter whether they’re functional like Gyroscope features menu, technology demonstrators like the amazing periodic table demo from famous or they’re artistic representation pushing the limits of...