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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Within the function we first create some <strong>of</strong> the elements that make up our marquee<br />

including the outer container, the heading and two purely aesthetic elements used<br />

to add the left and right fade effect at the start and end <strong>of</strong> the row <strong>of</strong> links. All <strong>of</strong> these<br />

elements are stored in variables so that we can access them easily when required.<br />

Next we process the JSON object passed into the function. Remember, this object contains<br />

a series <strong>of</strong> properties where the values <strong>of</strong> some <strong>of</strong> these properties are arrays, such as the<br />

posts array, which contains each <strong>of</strong> the returned posts as objects within each <strong>of</strong> its<br />

array items.<br />

We use a for loop to iterate over each object in the posts array that is returned with the<br />

JSON object. This object contains a property called count, where the number <strong>of</strong> posts that<br />

are returned is stored as an integer, so we can use this to tell the for loop how many times<br />

to execute, which is marginally easier than counting the objects in the posts array.<br />

For each post that has been returned, we create a new element, setting its href to<br />

point to the url property <strong>of</strong> the current object, and the title and text <strong>of</strong> the element set<br />

to the title property <strong>of</strong> the current object, and then append the new to the marquee<br />

element that we created a minute ago.<br />

Once we've created and appended a link for each post, we then wrap the contents <strong>of</strong> the<br />

marquee element (the links) in a new , prepend the to the start <strong>of</strong> the marquee,<br />

and append the elements for the fades to the end <strong>of</strong> the marquee element. We then<br />

append the marquee to the page before sliding it into view with the slideDown() method.<br />

At this point we need to set a width on the container that we wrapped the links in<br />

a moment ago. This is so that the links can all line up in a single row. We need to take into<br />

account the width <strong>of</strong> each link, plus any margin it has (which we set in the CSS).<br />

We use a function as the value <strong>of</strong> <strong>jQuery</strong>'s width() method to iterate over each link and<br />

add its width and margin to a running total. We can't do this until the marquee has been<br />

appended to the page because it is not until this point that each element actually has a<br />

width or margin that we can retrieve.<br />

The last thing we do in the callback function for our getSJON() method is fire <strong>of</strong>f a custom<br />

event with the trigger() <strong>jQuery</strong> method. The custom event is called marquee-ready<br />

and is used to tell our script when the marquee has been added to the page. We'll use this<br />

custom event shortly to animate the post links.<br />

We also added some new CSS to our stylesheet. Some <strong>of</strong> this code is to give our marquee<br />

elements the same light skin as the rest <strong>of</strong> the page. But other parts <strong>of</strong> it, such as floating the<br />

links, and setting the marquee's overflow property to hidden is so that the links line up in<br />

a single row, and so that the majority <strong>of</strong> the links are hidden, ready to be scrolled into view.<br />

We also add the fade images to the last two elements inside the marquee element.<br />

[ 226 ]

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

Saved successfully!

Ooh no, something went wrong!