Background Animations Using MooTools

By on  

Updated 4/25/2011: The background animation has been updated to work well with Internet Explorer 7 and 8, and more efficiently with every other browser. This was accomplished by adding the wait property to the animation, and using an initial setStyle assignment to work around an IE8 background-position bug.

MooTools background animation

One of the sweet effects made easy by JavaScript frameworks like MooTools and jQuery is animation. I ran across this great jQuery tutorial that walks you through animating a background image of a page. Here's a quick MooTools code snippet that shows you how you can add this sweet effect to any element on a page.


#animate-area	{ 
	background-image: url(clouds.png);
	background-position: 0px 0px;
	background-repeat: repeat-x;

The first step is assigning the image as a background image for our given container. Be sure to repeat the background horizontally!

The MooTools JavaScript

window.addEvent("domready",function() {
	var duration = 40000;
	var length = 2000;
	var count = 0;
	var tweener;
	// Executes the standard tween on the background position
	var run = function() {
		tweener.tween("background-position", "-" + (++count * length) + "px 0px");
	// Defines the tween
	tweener = $("animate-area").setStyle("background-position", "0px 0px").set("tween", { 
		duration: duration, 
		transition: Fx.Transitions.linear,
		onComplete: run,
		link: "cancel"
	// Starts the initial run of the transition

The first step, as always is getting our settings ready for the show. The next piece is putting the animation function in place. We increment the negative background left position counter calculation to keep the show rolling. Last step is playing the show!

Make sure the animation speed is very slow and subtle -- a rapid background speed could make your users pass out. On the other hand, implementing it tastefully will make your website unique.

Track.js Error Reporting

Recent Features

  • Detect DOM Node Insertions with JavaScript and CSS Animations

    I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API....

  • Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • HTML5’s placeholder Attribute

    HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the...

  • Truly Responsive Images with responsive-images.js

    Responsive web design is something you hear a lot about these days. The moment I really started to get into responsive design was a few months ago when I started to realise that 'responsive' is not just about scaling your websites to the size of your...


  1. senshikaze

    awesome look, though i can see some people going overboard with this.

  2. Steven Schrab

    What? No Parallax effect? :P

  3. This seems cool but may difficult to successfully implement into an effective design. It seems quite limited as well but don’t get me wrong, it’s a great way to animate a background with the simplicity of moo tools. I would suggest looking at snapjay.com and the code be hide it at code.snapjay.com…

  4. cake

    nice work. Could you convert sich special nice effect from jQuery to Mootools. This is an awesome effect.

  5. @cake
    There is a plugin for mootools called mparallax, but its developer’s website (Piksite.com) is down for some reason….

  6. I’ve been meaning to look into fading background images from one-to-another with jquery/mootools.. don’t suppose you’d be willing to show how that’d work? Cheers, David.

  7. @Paul Winslow:

    Paul you should check the site I provided above: code.snapjay.com

    You may find that jquery useful, but on snapjay.com he uses a background fader. I will be writing a article on this later tonight. I will let you know…

  8. Hi David! Cool to see this work in Mootools! I’m using a Jquery version of this on my company site (Mediashake.nl). Works pretty good, but I wish I could save it’s state so the clouds won’t restart for each new page or refresh… any idea’s?

  9. I agree with Paul Winslow…fading background images into each other would make an awesome experiment!

  10. Hello David!

    Great thanks for your acceptance for the work ! Your are Cool!



  11. Nice sample, parallax version would be nice too ;-)
    To bad that subpixel movment is not possible (is it?), for slow & smoother animations. Can´t wait until Canvas is possible on everyone’s browser.

  12. Richard

    Great work indeed!

  13. Amos

    Wouldn’t it be a lighter and simpler solution to have a GIF image instead?

  14. derdummkopf

    sorry david,

    ie7 shows no animation

  15. I agree that you want the animation subtle. This is after all the header and not the main content. I have an animated cloud on my site. This is a link to the site with the animated cloud and also a tutorial….


  16. Hi David,

    IE 8 = no animation :-(


  17. Roark

    Hi David!

    Great tutorial as usual…

    Please would you give me a hand in 1.11’a fying this code? Joomla! 1.5 as you may know uses the older library so when I create a Joomla site I need to use 1.11 compatible code.

    Any help would be greatly appreciated!

  18. Roark

    Damn I’m good… :)

    window.addEvent('domready',function() {
    var duration = 5000;
    var length = 2000;
    var count = 0;
    var fx2 = new Fx.Style($('bannerwrap'),'background-position', {duration: duration, transition: Fx.Transitions.linear, wait:false});
    var run = function() {
      fx2.start('-' + (++count * length) + 'px 24px');

    You feel better when you do it yourself, only lost a couple hours getting it right.
    damn ‘wait’ option

  19. tim_keffer

    I get where the background: coding goes, but where does the the action content lay in??

    Thank you !!

  20. George Felix

    Hello, im test this script running Version 1.11 under ie8 but have error push is null, maybe anybody know solution?

    Thanks in advance. =)

  21. Chris TC

    I tried this code successfully with a background positioned on top of a div, but when i try position it on bottom, it seems like the code make it go up little by little, funny but... wasn't really expected. Is there a way to correct this effect ?

  22. Andy

    oncomplete: run is a very clever way to do it, nice! I would have gone with some nasty settimeout solution.

  23. For everyone asking for parallax, it’s very simple. I’ve put the functions into a class to make this easier:

    bgScroller = new Class({

    Implements: [Events, Options],

    options: {
    duration: 40000

    tweener: null,
    length: 0,
    count: 0,
    verticalPosition: null,

    run: function() {
    this.tweener.tween('background-position', ('-' + (++this.count * this.length) + 'px ' + this.verticalPosition));

    initialize: function(element, options){
    this.tweener = element;
    this.length = this.tweener.getSize().x;
    this.verticalPosition = this.tweener.getStyle("background-position").split(" ")[1];
    this.tweener.setStyle("background-position", ("0px " + this.verticalPosition));
    this.tweener.set('tween', {
    duration: this.options.duration,
    transition: Fx.Transitions.linear,
    onComplete: this.run.bind(this),
    wait: false


    Then all you have to do is set separate instances of the class to each layer:

    window.addEvent("domready", function() {
    var frontScroller = new bgScroller($("front"), { duration: 9000 });
    var middleScroller = new bgScroller($("middle"), { duration: 85000 });
    var backScroller = new bgScroller($("back"), { duration: 55000 });

    • This is great Brad, but I had a small mistep in my original code. wait: false should be link: "cancel"

    • Ah, yes, I see. Easy mistake, since wait is part of the MooTools system. It’s just an extra for Class.chain.

      Feel free to edit that into my comment.

      I put together a demo, but I won’t be able to get it online until later.

  24. Hello I need help getting the background. I use a joomla template and I am not able to.
    To place the image to and no problem.
    But do not get it to move.
    I use CSS file.

  25. Cheta

    Can’t make it to work with IE 7,8 9 . Works fine with chrome and ff. IE gives error that object doesn’t support the property.. Please help me ..

  26. Toadward

    Hi David,

    thanks for the tutorial, it’s great.

    A little problem is the use of “(++count * length)” to define the tweening target. As this runs endless some desktop browsers are stopping the animation effect after some minutes.
    I think it would be better to set the target to “length” and reset the background-position in the “run” function.

  27. anand

    Thanks for the tutorials.
    I am looking for codes of js,html5,css3 without flash for the same animation. (I mean i want to make a animation with 3 languages) Can anybody please help me? thanks in advance.

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

  • OSCON Portland:  Conference  Discount!

    O'Reilly puts on the best web industry conferences in the world.  These conferences include Fluent Conference, Velocity Conference, and the upcoming OSCON in Portland, Oregon from July 20-24.  Open Source Convention (OSCON) is a conference that focuses specifically on open source developers and the tools and possibilities...

  • Follow Redirects with cURL

    I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does. I recently cURL'd the Google homepage and saw the following: I found it weird that Google...

  • Developers Have WordPress, Amateurs Have Squarespace, Professional Designers Have the NEW Webydo!

    Web design platforms have traditionally come in one of two varieties. There are the solutions like WordPress and Drupal that are incredibly powerful, but an understanding of web development and coding is required to be able to use those platforms effectively. On the other side of the...

  • 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...