CSE 190M Web Programming

Lecture 21: Animations / jQuery UI

Reading: None

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.

Valid HTML5 Valid CSS

Visual Effects

jQuery UI overview

jQuery UI : a JavaScript library, built on top of jQuery, that adds:

Downloading and using jQuery UI

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

Visual effects

(appearing)



(disappearing)

(Getting attention)

jquery ui logo
Click effects above

Applying effects to an element

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");

Effect options

element.effect(effectName, {
	option: value,
	option: value,
	...
});
$("#myid").effect("explode", {
	"pieces": 25
});

Normal

25 Pieces

Effects chaining

$('#demo_chaining')
	.effect('pulsate')
	.effect('highlight')
	.effect('explode');

Effect duration

$('#myid').effect('puff', {}, duration)

Custom effects - animate()

$('#myid').animate(properties, [duration]);
$('#myid')
	.animate({
		'font-size': '80px',
		'color': 'green'
	}, 1000);

This is a demo!!!

Custom effects easing

$('#myid')
	.animate(properties, [duration], [easing]);
$('#myid')
	.animate({
		'font-size': '80px',
		'color': 'green'
	}, 1000, 'easeOutBounce');

This is a demo!!!

Effects Easing

Better Custom Effects* - toggleClass()

This is a demo!!!

Adding delay()

$('#myid')
	.effect('pulsate')
	.delay(1000)
	.slideUp()
	.delay(3000)
	.show('fast');

Effect complete event

$("#myid").effect('puff', [options], [duration], [function]);
$('#myid').effect('clip', {}, 'default', function() {
	alert('finished');
});

Good effects syntax

Sortable Lists; Drag and Drop

Drag and drop

jQuery UI provides several methods for creating drag-and-drop functionality:

Sortable

$('#myid ul').sortable([options]);

Sortable demo

<ol id="simpsons">
	<li>Homer</li>
	<li>Marge</li>
	<li>Bart</li>
	<li>Lisa</li>
	<li>Maggie</li>
</ol>
$(function() {
	$("#simpsons").sortable();
});
  1. Homer
  2. Marge
  3. Bart
  4. Lisa
  5. Maggie

Sortable list events

event 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
		}
	});
});

Sortable 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');
}
  1. Homer
  2. Marge
  3. Bart
  4. Lisa
  5. Maggie

Sortable "methods"

$('#my_list').sortable('methodName', [arguments]);

// Some examples
$('#my_list').sortable('destroy');
$('#my_list').sortable('option', 'cursor', 'pointer');

Draggable

$('#myid').draggable([options]);

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]
});
logo Draggable demo 1.
Default options.
logo Draggable demo 2.
{snap:[60, 60], revert:true}

Droppable

$('#myid').droppable([options]);

Drag/drop shopping demo

<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'));
}
shirt cup

Auto-completing Text Fields

Auto-completing text fields

autocomplete

Scriptaculous offers ways to make a text box that auto-completes based on prefix strings *:

* (won't be necessary once HTML5 datalist element is well supported)

Using a local autocompleter

var data = ["foo", "food", "foobar", "foolish", "foiled", "cake"];
$('#myid').autocompleter({
	'source': data
});

Local autocompleter demo

<input id="bands70s" size="40" type="text" />
<div id="bandlistarea"></div>
$('#bands70s').autocomplete({
	'source': data,
	'appendTo': '#bandlistarea'
});
Specify a 70's band:

Using an AJAX autocompleter

$('#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);

Other Features

accordion widget

<div class="accordion">
	<h1><a href="#">Section 1</a></h1>
	<div>Section 1 Content</div>
	...
</div>
Section 1

Mauris mauris ante, blandit et.

Section 2

Sed non urna. Donec et ante.

Section 3

Nam enim risus, molestie et, porta ac.

Section 4

Cras dictum. Pellentesque habitant morbi.

tabs widget

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

jQuery UI theming

kind classes
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

Applying UI styles

<link
	href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/theme/jquery-ui.css"
	rel="stylesheet" type="text/css" />