- 1 FAQ
- 2 Available methods
- 3 Examples
- 4 License
$fx() is a tiny, self-contained Javascript library for animating HTML elements. It doesn't require any other library to function and [should] play well with any other libraries you are using (Prototype, JQuery, Moo tools, etc.)
$fx() allows you to alter any CSS property along a timeline, allowing animated effects to play in succession — i.e. in the order you want them to. It handles all of the headaches of timer controls and makes it easy to animate anything from simple, subtle animations (see the clouds on the front page) to more elaborate animations (send in your example to get it linked here!).
$fx() also offers the ability to combine adjustments to CSS properties for combined effects, and allows you to set multiple callbacks, offering more flexibility.
... when there are so many other Javascript libraries to choose from? Simple. In a community where Javascript libraries are like free samples at Costco, it can be hard to choose just one. Most major Javascript libraries offer a huge list of features and effects that give you the ability to do just about anything in a browser that you could ever want. Unfortunately, with such a broad scope, you will often find that a great deal of the library will go unused and — furthermore — if you're just looking for some sweet animations, then you wont need 20k-60k of extra Javascript code offering things like AJAX controls, object-oriented elements, etc. You just want animation.
The reason you should use $fx() is that it's tiny and doesn't require any other library to function. With an additional 4k (small enough that even a 14.4bps modem from 20 years ago could download it in 3.5 seconds) for your site, you can be animating elements without needing to depend upon Flash, Silverlight, etc. an can stick to what you do best: making websites!
I looked around on the internet to find a simple Javascript animation library (much like you were, probably) and found that every option required tens of K just for the library, and that the vast majority of the features they offered were features I didn't need. Many of these Javascript libraries were larger than the entire site I was working on (including graphics!) which seemed unnecessary to me.
So, I set out to give the world something it desperately needed: a tiny, efficient, fast, simple and easy to use Javascript animation library. Soon afterward, $fx() was born.
Now, $fx() is not here to compete with any other Javascript Library, it's simply here to fill a gap.
If it involves animating DOM elements by altering CSS properties on a timeline, then probably yes. If not, then with your feature request, and I'll see what we can do!
If you want to share some ideas on how to make $fx() better, or have if you have any questions or comments, then and I'll reply as soon as I can.
$fx(element) —the primary class, which extends an element with the fxAdd(), fxAddSet(), fxHold(), fxPause(), fxReset(), fxRun(), fxStop() methods to describe and run effects, and contains an internal _fx property which contains the whole animation processing logic. (Basically, this is the Big Kahuna. You need this one.)
element — can either be a DOM element, or element id passed as "#elementId".
Returns modified DOM element. All added fx* methods return elements themselves.
fxAdd(params) — adds a new effect to the current set. The effects wont run immediately, but will be added to your animation set. The entire set will run after the fxRun() call is made.
params object sets animation parameters. Required set of fields to be defined:
opacity — cross-browser property to adjust an element's opacity.
backgroundx — background X position.
backgroundy — background Y position.
First you will need an element. Any element will do (let's assume it's: <div id="myDiv">). You then call $fx() and pass reference to the element as described above.
$fx('myDiv')
Then, you can add effects by calling to $fxAdd(...).
$fx('myDiv').fxAdd({type: 'fontSize', from: 12, to: 72, step: 1, delay: 20});
Then run it!
$fx('myDiv').fxAdd({type: 'fontSize', from: 12, to: 72, step: 1, delay: 20}).fxRun(null,-1);
You can follow the same steps of the basic animation example in order to do even the most elaborate of effects. So, let's hit the ground running and create a starfield, shall we?
We have two DIV's with same background image but different background-position properties. We will scroll backgrounds (alter background-position X and Y) from right to left width different speeds (for a Parallax Scrolling effect). As you can no doubt guess, background position animation is achieved by changing the backgroundx CSS Property.
To make cycled animations, we just run it with number of loops -1 (line 5,6). null, as you'll remember, means we are not defining any callbacks... we're just running the current animation set.
The fist DIV's background-X position is 0, so we wo;; scroll it to X=-314 and then bring it back to 0, so our animation will loop smoothly. But another one has initial background-X position of 45px, so it's end X coord should be -314 + 45 (or -269). $fx library doesn't take care of this [yet?], it just alters the settings you provide.
So, you will have to crunch a few numbers. ... oh, come on, it wouldn't be fair to do *all* the work for you!
So, we have our starfield. Now, let's add a spaceship, shall we?
ufoRotate scrolls the background image (the sprite with four sections)
ufoMove moves our little spaceship from left to right.
ufoAppear makes it to appear.
Once we call fxRun()
, the effects set gets closed and all following fxAdd() calls will add effects to the next set (which needs another fxRun() to start). You can see this in lines 9, 10.Also, notice how we call fxAdd() twice in line 10? That means that those two effects will be running parallel to each other, so our spaceship appears and will keep moving at the same time. Pretty sweet, huh?
A
B
All callbacks are applied to the element you are animating, so it lets us use the this reference. Take a look at the lines 12-24. Go ahead. I'll wait.
At the line 12, we just get references to the message bubbles. Onloop and onfinish are handlers we will use as callbacks.
A
B
Started
Easy enough!