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



<!-- If you want the default ui widget stylings -->
	rel="stylesheet" type="text/css" />

Visual effects



(Getting attention)

jquery ui logo
Click effects above

Applying effects to an element

element.effect();            // for some effects
element.effect(effectName);   // for most effects

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


25 Pieces

Effects chaining


Effect duration

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

Custom effects - animate()

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

This is a demo!!!

Custom effects easing

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

This is a demo!!!

Effects Easing

Better Custom Effects* - toggleClass()

This is a demo!!!

Adding delay()


Effect complete event

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

Good effects syntax

Sortable Lists; Drag and Drop

Drag and drop

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


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

Sortable demo

<ol id="simpsons">
$(function() {
  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() {
		'update': function(event, ui) {
			// Do stuff here

Sortable list events example

$(function() {
		'update': listUpdate

function listUpdate(event, ui) {
	// can do anything I want here; effects, an Ajax request, etc.
  1. Homer
  2. Marge
  3. Bart
  4. Lisa
  5. Maggie

Sortable "methods"

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

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



Draggable example

<div id="draggabledemo1">Draggable demo 1. Default options.</div>
<div id="draggabledemo2">Draggable demo 2.
	{'grid': [40,40], 'revert': true}</div>
	'revert': true,
	'grid': [40, 40]
logo Draggable demo 1.
Default options.
logo Draggable demo 2.
{snap:[60, 60], revert:true}



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>
	'drop': productDrop

function productDrop(event, ui) {
	alert("You dropped " + ui.item.attr('id'));
shirt cup

Auto-completing Text Fields

Auto-completing text fields


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"];
	'source': data

Local autocompleter demo

<input id="bands70s" size="40" type="text" />
<div id="bandlistarea"></div>
	'source': data,
	'appendTo': '#bandlistarea'
Specify a 70's band:

Using an AJAX autocompleter

	'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>
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">
		<li><a href="#tab1">Tab 1</a></li>
		<li><a href="#tab2">Tab 2</a></li>
	<div id="tab1">Tab 1 Content</div>
	<div id="tab2">Tab 2 Content</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

	rel="stylesheet" type="text/css" />