JavaScript Script.aculo.us Fade with a Timer

Learned a new JavaScript thing today: using a timer with the Script.aculo.us library!

It’s nothing terribly fancy but it took me a little to track down everything and in the end it was so easy.

I used a simple JavaScript timer to trigger a function that faded text out.

HTML & JavaScript

<script type="text/javascript">
    Event.observe(window, 'load', function() {
        var fade=setTimeout("fadeout()",3500);
        var hide=setTimeout("$('message').hide()",4800);
    });
    function fadeout(){
        new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
    }
</script>

<div id="message">Your email has been sent</div>

I’ll walk you through the process I used to create this snippet.

Create the Fade Out Function

I used the Script.aculo.us opacity effect in a simple JavaScript function that went from 100% opaque to 0% opaque in 1.5 seconds.

JavaScript

function fadeout(){
    new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
}

Start JavaScript Timer on Page Load

Next I used the Prototype Observe Event to get the timer started once the page has finished loading and then hide the div using Prototype’s hide function just before the text fades all the way out.

JavaScript

Event.observe(window, 'load', function() {
    var fade=setTimeout("fadeout()",3500);
    var hide=setTimeout("$('emailmessage').hide()",4800);
});
  • Page loads at 0 milliseconds
  • Text begins to fade out at 3500 milliseconds (3.5 seconds after the page finishes loading)
  • Text is hidden at 4800 milliseconds (4.8 seconds after the page finishes loading)
  • Text finishes it’s fade at 5000 milliseconds (5 seconds after the page finishes loading)

Pretty easy and very effective for feedback that a person only needs for just a moment and requires no additional action.

Read more about JavaScript timers at W3C Schools.


Prototype 1.6 Delay() Update

Updated 2007 Dec 19

Yesterday I noticed that Prototype made some major updates to the library in November so I thought I’d update the info in this tutorial to incorporate the new Prototype delay() function.

You can replace the HTML / JavaScript combination above with the following.

HTML & JavaScript

<script type="text/javascript">
    Event.observe(window, 'load', function() {
        fadeout.delay(3.5);
        Element.hide.delay(4.8, "message");
    });
    function fadeout(){
        new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
    }
</script>

<div id="message">Your email has been sent</div>

Doesn’t really reduce the space or runtime as far as I know but it’s an update. It’s nice to stay current with new features because you never know when you’ll find a use for them that far surpasses the old way of thinking.


Summary

Prototype Functions Used

Script.aculo.us Functions Used

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 2 comments… read them below or add one }

Kawsar September 4, 2008 at 1:01 pm

I am working on the design of the above website. so you will not see the issue that I’m having with the website.
On my new website I am using a video which is a large file so every time I go to the website it takes long time to load the page and background image. I used your javascript timer on page load code on the video clip. it’s working now. It doesn’t take long time that it used to but there is a runtime error saying “event is undefined”.
I am very new in javascript. Would you please help me to fix the problem. Thank you

Reply

Mark June 11, 2009 at 9:57 am

I know this is an older blog and I stumbled upon it from another site – looking for a fade code to use with a timer. My question is this: How do you account for the different browsers out there that may or may not opperate the same? IE has a different setup than FF or Safari. So how do you incorporate a browser detect function with your timer?
-mb

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: