Professional Documents
Culture Documents
*Contents
.animate
.hide
*Animation Queue
.slideDown
*Chaining Actions
.fadeOut
.delay
.scrollTo
.append
*return false
.jScrollPane
.resize (Event)
.width
.remove
.resizable (Plug-in)
Animating
Animating CSS Properties
$('p').animate({
padding: '20px',
borderBottom: '3px solid #8f8f8f',
borderRight: '3px solid #bfbfbf'
}, 2000);
Animate Parameters - Has to be
= Key/Value Pairs
= camelCase
*The values you define can be relative to the elements current
values: all you need to do is specify += or -= in front of the
value, and that value will be added to or subtracted from the
elements current property.
$('p').animate({
padding: '20px',
Color Animation
Unlike a height or width value that moves from one value to another in a simple,
linear manner, jQuery needs to do some extra math to figure out what color is,
say, three-quarters of the way between light blue and orange.
This color-calculating functionality is omitted from the core library. This makes
sense when you think about it: most projects have no need for this functionality,
so jQuery can keep the size of the core library to a minimum. If you want to
animate color, youre going to need to download the Color Animations plugin.1
Easing
$('p:first').toggle(function() {
$(this).animate({'height':'+=150px'}, 1000, 'linear');
}, function() {
$(this).animate({'height':'-=150px'}, 1000, 'swing');
});
Advanced Easing
<h3>Beau Dandy</h3>
= Click Event Tied to <h3>s
<div>
<img src="../../images/beau_100.jpg" width="100" height="100 "/>
<p>Content about Beau Dandy</p>
</div>
<h3>Johny Startdust</h3>
<div>
<img src="../../images/johnny_100.jpg" width="100" height="100 "/>
<p>After smashing into </p>
</div>
<h3>Glendatronix</h3>
<div>
<img src="../../images/glenda_100.jpg" width="100" height="100 "/>
<p>Content about Glendatronix</p>
</div>
</div><!--end #bio-->
$(function() {
$('#bio > div').hide();
//Hide any <div>s that are Children of #bio
$('#bio > div:first').show();
//Contents Visible of 1st <div> in #bio
$('#bio h3').click(function() { //Click <h3> & then its Next Sibling
$(this).next().animate(
= a <div> w/ <img> & <p> - becomes Visible
{ height: 'toggle'}, 'slow', 'easeOutBounce' by Toggling its Height
);
});
});
You can accomplish almost all of this with the simpler format
that weve already seen. You only need the advanced version
if you want to specify additional settings, like the queue
parameter.
The queue is the list of animations waiting to occur on a
particular element. Every time we ask jQuery to perform an
animation on an element, that animation is added to the
queue. The element executes the queue one at a time until
everything is complete
Chaining Actions
Chaining links two or more jQuery actions into a single statement.
- eg.
$('p:first').hide().slideDown('slow').fadeOut();
Its often good to lay out your actions on separate lines for
clarity
$('p:first')
.hide()
.slideDown('slow')
.fadeOut();
.delay()
$('p:first')
.hide()
.slideDown('slow')
.delay(2000)
.fadeOut();
Animated Navigation
1. Ex = Nav Hover (Wobbling) Blob
lets apply our newly discovered jQuery power to create a Flash-like
navigation bar. It will have a background blob that wobbles around to
highlight the menu choice the user is hovering over.
Our background color blob will be an empty div element, positioned behind
whichever navigation link the user is mousing over.
1. Our first task, therefore, will be to create the element and append it to the
document:
$('<div id="navigation_blob"></div>').css({
width: $('#navigation li:first a').width() + 10,
height: $('#navigation li:first a').height() + 10
}).appendTo('#navigation').hide(); = Hidden until Hover
2. = 2nd Task => Hover Event
-
20px
});
.animate(
{height: '20px'},
Scrolling
The scroll Event
So, every time one of your users interacts with a scroll bar on
your site, an event will be fired that you can catch and respond
to.
#news {
}
$('#news').scroll(function() { = <div> w/ Latest News
$('#header').append(
= Add to End of #header
'<span class="scrolled">You scrolled!</span>');});
Try scrolling in different ways: dragging the scroll bar, using
the mouse wheel, and clicking inside the scrollable zone to use
the arrow keys. In all the above cases, the scroll event will fire.
Floating Navigation
In Ex- main navigation element is always present at the top of
the visible part of the screen, regardless of the scroll position;
This is easy to implement using the scroll event: we simply
need to find out where we are on the page, and then move the
navigation to that point.
Our first task is to set up our CSS to prepare for the animated
navigation. Well add a position: relative;declaration to our
navigation element so that we can easily move it up and down
the page by adjusting its top property. For the purpose of this
exercise, weve applied a very large height property to the content
in order to force it to have a scroll bar
#navigation {
position: relative;
}
#content { height: 2000px;} = to Force a Scroll Bar- 4 Ex
$(window).scroll(function() {
$('#navigation').css('top', $(document).scrollTop());
});
.scrollTop
= Returns Top offset of = Matched El
To know where the top of the screen is we used the scrollTop
action. scrollTop returns the top offset of the matched
elementin our example we asked for the entire documents
Resizing
The resize Event
$(window).resize(function() {
alert("You resized the window!");
});
Layout Switcher
*In Ex = two separate, fixed-width layouts, and switch between
them by catching the resize event.
1. CSS
- Add body to Selectors to make = more
Specific (to override Existing Styles)
body #container { width:
850px;}
body #container p
{ width: 650px;}
body #header { background-image: url('../../css/images/header-corners-wide.png');}
body #celebs table { width: 650px; margin-left: 5px;
}
The next step = write the jQuery code to add or remove our new
style sheet.
if ($('body').width() > 900) {
$('<link rel="stylesheet" href="wide.css" type="text/css" />')
.appendTo('head');
} else {
$('link[href=wide.css]').remove();}
Resizable Elements
The jQuery UI library contains a Resizable plugin as part of its
interaction functionality. The Resizable plugin makes elements that
you select able to be resized by adding a small handle (These are
Resizable textarea
$('textarea').resizable({ = 2 Resize Input Area
grid : [20, 20],
minWidth : 153,
minHeight : 30,
maxHeight : 220,
containment: 'parent'
= instead of maxWidth
});
- so can be as Wide as its Parent
The Resizable component is flexibile. It has a plethora of
configuration options, which you can explore in more detail on
the jQuery UI documentation site
Youll notice that weve omitted the maxWidth property in favor of the
containment parameter: this lets you specify a container restricting the
resizable element. You can use either a jQuery selector as the parameter or
the special keyword parent to refer to the resizable elements parent
element.
Weve also used the grid option to confine the resizable object to steps of a
certain size. For some reason, this seems to add a nice feel to the resizing
interaction. The
grid is specified as an array containing two elements: the horizontal grid size
and the vertical grid size.
One other parameter youll want to look into is the handles parameter. This
specifies which sides of the element the handles will be attached to and,
consequently, in which directions the element can be stretched. The
parameter accepts the following options: n, e, s, w, ne, se, sw, nw, and all. You
can specify any number of these by separating them with a comma. For
example, { handles : 'n', 'se'}adds handles to the top and bottom-right of the
element.
Pane Splitter
*In Ex =
to Display the companys terms and conditions. The problem is
that there are pages and pages of terms and conditions divided
into many subsectionsyet they need to be prominently
Every resizing will now also trigger a change in the second elements
width. A bit of basic math helps us work out what the widths should be: we
take the parent containers width (that is, the total width), then subtract the
first divs outerWidth. The outerWidth function is a useful way to grab the total
width of an element, including any padding and borders (it can also include
margins if you pass it the optional parameter true). Perhaps unsurprisingly, theres
a corresponding outerHeight function as well.
Having calculated how much space is left to use up, were almost ready to
set the first elements width. Theres just one remaining catch: if the
second div has borders or padding, we need to take these into consideration.
Unfortunately the outerWidth function is read-only, so were unable to use
it to set the total height.
To calculate how much of the elements width consists of borders and
padding, we need to subtract the elements outerWidth from its width.
Subtracting that from the remaining Space variable gives us exactly how
many pixels wide the second div needs to beand we can complete our
horizontal splitter.
These simple splitters are quite useful, require very little code, and will be
suitable for many purposes. But if you require complex splitter behavior, such
as multiple split panes or nested panes, head over to the plugin repository
and check out the jQuery Splitter plugin.