jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

}), 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 ]

}),<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.

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

Saved successfully!

Ooh no, something went wrong!