jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
}), pointerText = "Use your pointer to scroll, moving to the edge scrolls faster!", keyboardMessage = "Use your arrow keys to scroll the images!", message = $("", { id: "message", text: keyboardMessage }); prox.addClass("slider").wrapInner(scroller).append(message); var middle = prox.width() / 2; scroller = $("#scroller"); scroller.width(function() { var total = 0; scroller.children().each(function(i, val) { var el = $(this); total = total + (el.outerWidth() + parseInt(el.css("marginLeft"))); }); return total; }).css("left", "-" + (scroller.width() / 2 - middle) + "px"); [ 211 ] Chapter 8 What just happened? First we cache the selector for the proximity container, which we'll use a couple of times in this chunk of code, and a couple of times a little later on in the script. Next we create a new element and give it an id attribute so that we can easily select it again when necessary. We also use this id for styling purposes. Next we store a couple of text strings in variables for convenience. These will be used as messages to display to the visitor at different points. We also create a new paragraph element as a container for the message text, give the element an id (again for selecting purposes), and use the jQuery text() method to set its innerText to one of the text strings. Using jQuery 1.4 syntax we can use the property text on the object passed as the second argument to the element creation jQuery method format, which automatically maps to the text() method.
Other Popular Animations Next we add a class name to the outer proximity container. Remember, this class name is used to differentiate between scripting being disabled and enabled so that we can add the required styling. We also wrap the contents of the proximity container (the 20 images) in our newly created scroller element, and append the message to the proximity container. Next we set a variable which is equal to the width of the proximity container divided by two, which gives us the horizontal middle of the element, which we'll need to use in some calculations to position the scroller element, and work out where the mouse pointer is relative to the proximity container. We could just as easily have set the number that the middle variable needs to contain, instead of calculating it in this way. The width of the proximity container (with scripting enabled) is set in our CSS file and is highly arbitrary to this particular example. If we changed its width however, the script would break if we set the figure directly in the variable instead of working it out programmatically. It is always best to avoid hardcoding 'magic' numbers into scripts whenever possible. At this point we also need to cache a reference to the scroller element now that it has been appended to the page. We can't use the contents of the scroller variable that we created at the start of the script, so we overwrite it with a fresh reference to the element by selecting it from the page again. We now need to set the width of the scroller element so that it is wide enough to accommodate all of the images in a single row. To do this we pass a function to jQuery's width() method which returns the width to set. The function calculates this figure by iterating over each image and adding both its width and horizontal margin to the total variable. This means that an indeterminate number of images can be used without changing the script, and that images with different widths and spacing can be used. Once we've set the width of the scroller element, we then need to position it so that the center of the scroller is at the center of the proximity container. This is so that when the page loads, the visitor can move it to the left or right depending on where they move their pointer or which arrow key is pressed. If we load the page in a browser at this point, we should find that the appearance of the elements on the page has changed: [ 212 ]
- Page 181 and 182: Extended Animations with jQuery UI
- Page 183 and 184: Extended Animations with jQuery UI
- Page 185 and 186: Extended Animations with jQuery UI
- Page 187 and 188: Extended Animations with jQuery UI
- Page 189 and 190: Extended Animations with jQuery UI
- Page 191 and 192: Extended Animations with jQuery UI
- Page 193 and 194: Extended Animations with jQuery UI
- Page 195 and 196: Extended Animations with jQuery UI
- Page 197 and 198: Extended Animations with jQuery UI
- Page 199 and 200: Extended Animations with jQuery UI
- Page 201 and 202: Full Page Animations Can we animate
- Page 203 and 204: Full Page Animations display:block;
- Page 205 and 206: Download from Wow! eBook Full Page
- Page 207 and 208: Full Page Animations Have a go hero
- Page 209 and 210: Full Page Animations 4. Save this f
- Page 211 and 212: Full Page Animations 2. The bgpos c
- Page 213 and 214: Full Page Animations nav ul { margi
- Page 215 and 216: Full Page Animations screensize = {
- Page 217 and 218: Full Page Animations What just happ
- Page 219 and 220: Full Page Animations The second sec
- Page 221 and 222: Full Page Animations Pop quiz - cre
- Page 223 and 224: Full Page Animations Time for actio
- Page 225 and 226: Full Page Animations Next we define
- Page 228 and 229: 8 Other Popular Animations This cha
- Page 230 and 231: [ 209 ] Chapter 8 } .slider #scroll
- Page 234 and 235: [ 213 ] Chapter 8 In the previous s
- Page 236 and 237: prox.mousemove(function(ev) { goAni
- Page 238 and 239: Pop quiz - implementing proximity a
- Page 240 and 241: [ 219 ] Chapter 8 What just happene
- Page 242 and 243: 2. Save the new page as marquee.htm
- Page 244 and 245: [ 223 ] Chapter 8 1. jQuery provide
- Page 246 and 247: clear:both; } div#fadeLeft, div#fad
- Page 248 and 249: Time for action - animating the pos
- Page 250: 2. In this example we used the dele
- Page 253 and 254: CSS3 Animations The following trans
- Page 255 and 256: CSS3 Animations Units It is of crit
- Page 257 and 258: CSS3 Animations Skew Remember the t
- Page 259 and 260: CSS3 Animations Calculating the sin
- Page 261 and 262: CSS3 Animations The filter property
- Page 263 and 264: CSS3 Animations If, like me, you th
- Page 265 and 266: CSS3 Animations } el.filters.item("
- Page 267 and 268: CSS3 Animations An unfortunate prob
- Page 269 and 270: CSS3 Animations The context of this
- Page 271 and 272: CSS3 Animations Time for action - i
- Page 273 and 274: CSS3 Animations The first variable
- Page 275 and 276: CSS3 Animations Time for action - a
- Page 277 and 278: Download from Wow! eBook CSS3 Anim
- Page 279 and 280: CSS3 Animations We also have to upd
- Page 281 and 282: CSS3 Animations flatMatrix[3] = fla
}),<br />
pointerText = "Use your pointer to scroll, moving to the edge<br />
scrolls faster!",<br />
keyboardMessage = "Use your arrow keys to scroll the images!",<br />
message = $("", {<br />
id: "message",<br />
text: keyboardMessage<br />
});<br />
prox.addClass("slider").wrapInner(scroller).append(message);<br />
var middle = prox.width() / 2;<br />
scroller = $("#scroller");<br />
scroller.width(function() {<br />
var total = 0;<br />
scroller.children().each(function(i, val) {<br />
var el = $(this);<br />
total = total + (el.outerWidth() +<br />
parseInt(el.css("marginLeft")));<br />
});<br />
return total;<br />
}).css("left", "-" + (scroller.width() / 2 - middle) + "px");<br />
[ 211 ]<br />
Chapter 8<br />
What just happened?<br />
First we cache the selector for the proximity container, which we'll use a couple <strong>of</strong> times<br />
in this chunk <strong>of</strong> code, and a couple <strong>of</strong> times a little later on in the script. Next we create a<br />
new element and give it an id attribute so that we can easily select it again when<br />
necessary. We also use this id for styling purposes.<br />
Next we store a couple <strong>of</strong> text strings in variables for convenience. These will be used<br />
as messages to display to the visitor at different points. We also create a new paragraph<br />
element as a container for the message text, give the element an id (again for selecting<br />
purposes), and use the <strong>jQuery</strong> text() method to set its innerText to one <strong>of</strong> the text<br />
strings. Using <strong>jQuery</strong> <strong>1.4</strong> syntax we can use the property text on the object passed as the<br />
second argument to the element creation <strong>jQuery</strong> method format, which automatically maps<br />
to the text() method.