Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
jQuery UI : a JavaScript library, built on top of jQuery, that adds:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js" type="text/javascript"></script> <!-- If you want the default ui widget stylings --> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
.js
files to your project folder
(appearing)
(disappearing)
(Getting attention)
element.effect(); // for some effects
element.effect(effectName); // for most effects
$("#sidebar").slideUp();
// No need to loop over selected elements, as usual
$("#results > button").effect("pulsate");
animate()
element.effect(effectName, { option: value, option: value, ... });
$("#myid").effect("explode", { "pieces": 25 });
Normal
25 Pieces
{}
)$('#demo_chaining') .effect('pulsate') .effect('highlight') .effect('explode');
duration
optionslow
, normal
, fast
or any number in milliseconds$('#myid').effect('puff', {}, duration)
animate()
$('#myid').animate(properties, [duration]);
$('#myid') .animate({ 'font-size': '80px', 'color': 'green' }, 1000);
This is a demo!!!
$('#myid') .animate(properties, [duration], [easing]);
$('#myid') .animate({ 'font-size': '80px', 'color': 'green' }, 1000, 'easeOutBounce');
This is a demo!!!
toggleClass()
toggleClass
method with its optional duration
parameter
.special { font-size: 50px; color: red; }
$('#myid').toggleClass('special', 3000);
This is a demo!!!
delay()
$('#myid') .effect('pulsate') .delay(1000) .slideUp() .delay(3000) .show('fast');
$("#myid").effect('puff', [options], [duration], [function]);
this
keyword as usual to address the element the effect was attached to$('#myid').effect('clip', {}, 'default', function() { alert('finished'); });
$('#myid').effect('pulsate', {}, 'default', function() { $(this).effect('explode', {'pieces': 25}, 'default', function() { $(this).show('slow', {}, 'default', function() { $(this).hide('fast'); }); }); });
$('#myid') .effect('pulsate') .effect('explode', { 'pieces': 25 }).show('slow') .hide('fast');This works because the effects are queued up rather than applied immediately.
jQuery UI provides several methods for creating drag-and-drop functionality:
Sortable
$('#myid ul').sortable([options]);
ul
, ol
) as being able to be dragged into any orderDraggable
s and Droppable
sdisabled
appendTo
axis
cancel
connectWith
containment
cursor
cursorAt
delay
distance
dropOnEmpty
forceHelperSize
opacity
revert
tolerance
.sortable('destroy')
on the sortable elementSortable
demo<ol id="simpsons"> <li>Homer</li> <li>Marge</li> <li>Bart</li> <li>Lisa</li> <li>Maggie</li> </ol>
$(function() { $("#simpsons").sortable(); });
Sortable
list eventsevent | description |
---|---|
change |
when any list item hovers over a new position while dragging |
update |
when a list item is dropped into a new position (more useful) |
$(function() {
$("simpsons").sortable({
'update': function(event, ui) {
// Do stuff here
}
});
});
ui
parameter with info about the eventSortable
list events example
$(function() {
$("#simpsons").sortable({
'update': listUpdate
});
});
function listUpdate(event, ui) {
// can do anything I want here; effects, an Ajax request, etc.
ui.item.effect('shake');
}
destroy
disable
enable
option
refresh
cancel
$('#my_list').sortable('methodName', [arguments]);
// Some examples
$('#my_list').sortable('destroy');
$('#my_list').sortable('option', 'cursor', 'pointer');
Draggable
$('#myid').draggable([options]);
Options:
disabled
appendTo
addClasses
connectToSortable
delay
distance
grid
Methods:
destroy
disable
enable
option
widget
Events:
create
start
drag
stop
Draggable
example<div id="draggabledemo1">Draggable demo 1. Default options.</div> <div id="draggabledemo2">Draggable demo 2. {'grid': [40,40], 'revert': true}</div>
$('#draggabledemo1').draggable(); $('#draggabledemo2').draggable({ 'revert': true, 'grid': [40, 40] });
Droppable
$('#myid').droppable([options]);
Options:
disabled
accept
activeClass
hoverClass
scope
greedy
tolerance
Methods:
destroy
disable
enable
option
widget
Events:
create
over
out
drop
activate
deactivate
<img id="shirt" src="images/shirt.png" alt="shirt" /> <img id="cup" src="images/cup.png" alt="cup" /> <div id="droptarget"></div>
$('#shirt').draggable(); $('#cup').draggable(); $('#droptarget').droppable({ 'drop': productDrop }); function productDrop(event, ui) { alert("You dropped " + ui.item.attr('id')); }
Scriptaculous offers ways to make a text box that auto-completes based on prefix strings *:
var data = ["foo", "food", "foobar", "fooly", "cake"]; $('#my_text_input').autocompleter({ 'source': data });
term
parameter with the current value of the input field
$('#my_text_input').autocompleter({ 'source': 'http://foo.com/webservice.php' });
* (won't be necessary once HTML5 datalist
element is well supported)
var data = ["foo", "food", "foobar", "foolish", "foiled", "cake"]; $('#myid').autocompleter({ 'source': data });
label
and value
fields
var data = [ {'label': 'Track and Field', 'value': 'track'}, {'label': 'Gymnastics', 'value': 'gymnastics'}, ... ];
ul
elements full of choices as you typeappendTo
option to specify where the list is inserted<input id="bands70s" size="40" type="text" /> <div id="bandlistarea"></div>
$('#bands70s').autocomplete({ 'source': data, 'appendTo': '#bandlistarea' });
$('#my_input').autocomplete({ 'source': 'http://foo.com/webservice.php' });
if (!isset($_GET['term'])) {
header('HTTP/1.1 400 Invalid Request - No term parameter provided');
die('No term parameter provided.');
}
$term = $_GET['term'];
$results = getCompleterResults($term); // an array() return value
print json_encode($results);
term
parameterlabel
and value
fieldsaccordion
widgetaccordion
<div class="accordion"> <h1><a href="#">Section 1</a></h1> <div>Section 1 Content</div> ... </div>
tabs
widgethref
attributes should match id
s of elements on the page<div class="tabs"> <ul> <li><a href="#tab1">Tab 1</a></li> <li><a href="#tab2">Tab 2</a></li> ... </ul> <div id="tab1">Tab 1 Content</div> <div id="tab2">Tab 2 Content</div> ... </div>
Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus.
Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante.
Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti.
class
es gratuitously so that we can style our widgets however we wantclass
es which exist for all widgetsclass
eskind | class es |
---|---|
Layout Helpers |
.ui-helper-hidden ,
.ui-helper-reset ,
.ui-helper-clearfix
|
Widget Containers |
.ui-widget ,
.ui-widget-header ,
.ui-widget-content
|
Interaction States |
.ui-state-default ,
.ui-state-hover ,
.ui-state-focus ,
.ui-state-active
|
Interaction Cues |
.ui-state-highlight ,
.ui-state-error ,
.ui-state-error-text ,
.ui-state-disabled ,
.ui-priority-primary ,
.ui-priority-secondary
|
ui class
es in your CSS to roll your own theme<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/theme/jquery-ui.css" rel="stylesheet" type="text/css" />