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 />

Time for action – creating an animated header<br />

The underlying page for this example will be relatively straight-forward, with just a<br />

element whose background-position we'll animate manually:<br />

1. The header <strong>of</strong> the example page will consist <strong>of</strong> just an empty element:<br />

<br />

<br />

2. Save this as animated-header.html. The CSS is even simpler, with just a single<br />

selector and a few rules:<br />

header {<br />

display:block; width:960px; height:200px; margin:auto;<br />

background:url(../img/header.jpg) repeat 0 0;<br />

}<br />

3. Save this as animated-header.css. We'll need to link to the file from the <br />

<strong>of</strong> the page we just created.<br />

4. The script itself is also surprisingly simple. Add the following code to the function at<br />

the end <strong>of</strong> the :<br />

var header = $("header");<br />

header.css("backgroundPosition", "0 0");<br />

var bgscroll = function() {<br />

var current = parseInt(header.css("<br />

backgroundPosition").split(" ")[1]),<br />

newBgPos = "0 " + (current - 1) + "px";<br />

header.css("backgroundPosition", newBgPos);<br />

};<br />

setInterval(function() { bgscroll() }, 75);<br />

5. When we run the file in a browser, we should find that the background image used<br />

for the slowly scrolls.<br />

[ 218 ]

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

Saved successfully!

Ooh no, something went wrong!