02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Other Popular <strong>Animation</strong>s<br />

Next we add a class name to the outer proximity container. Remember, this class name is<br />

used to differentiate between scripting being disabled and enabled so that we can add the<br />

required styling. We also wrap the contents <strong>of</strong> the proximity container (the 20 images) in<br />

our newly created scroller element, and append the message to the proximity container.<br />

Next we set a variable which is equal to the width <strong>of</strong> the proximity container divided by<br />

two, which gives us the horizontal middle <strong>of</strong> the element, which we'll need to use in some<br />

calculations to position the scroller element, and work out where the mouse pointer is<br />

relative to the proximity container.<br />

We could just as easily have set the number that the middle variable needs to contain,<br />

instead <strong>of</strong> calculating it in this way. The width <strong>of</strong> the proximity container (with scripting<br />

enabled) is set in our CSS file and is highly arbitrary to this particular example. If we changed<br />

its width however, the script would break if we set the figure directly in the variable instead<br />

<strong>of</strong> working it out programmatically. It is always best to avoid hardcoding 'magic' numbers<br />

into scripts whenever possible.<br />

At this point we also need to cache a reference to the scroller element now that it has<br />

been appended to the page. We can't use the contents <strong>of</strong> the scroller variable that we<br />

created at the start <strong>of</strong> the script, so we overwrite it with a fresh reference to the element by<br />

selecting it from the page again.<br />

We now need to set the width <strong>of</strong> the scroller element so that it is wide enough to<br />

accommodate all <strong>of</strong> the images in a single row. To do this we pass a function to <strong>jQuery</strong>'s<br />

width() method which returns the width to set.<br />

The function calculates this figure by iterating over each image and adding both its width<br />

and horizontal margin to the total variable. This means that an indeterminate number <strong>of</strong><br />

images can be used without changing the script, and that images with different widths and<br />

spacing can be used.<br />

Once we've set the width <strong>of</strong> the scroller element, we then need to position it so that the<br />

center <strong>of</strong> the scroller is at the center <strong>of</strong> the proximity container. This is so that when the page<br />

loads, the visitor can move it to the left or right depending on where they move their pointer<br />

or which arrow key is pressed.<br />

If we load the page in a browser at this point, we should find that the appearance <strong>of</strong> the<br />

elements on the page has changed:<br />

[ 212 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!