Top Banner
Building Interactive Prototypes with jQuery @Media Ajax - November 2007 John Resig (ejohn.org)
31

Building Interactive Prototypes with jQuery

Aug 20, 2015

Download

Technology

jeresig
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building Interactive Prototypes with jQuery

Building Interactive Prototypes with jQuery

@Media Ajax - November 2007John Resig (ejohn.org)

Page 2: Building Interactive Prototypes with jQuery

What is jQuery?✦ An open source JavaScript library that

simplifies the interaction between HTML and JavaScript.

Page 3: Building Interactive Prototypes with jQuery

Ideal for Prototyping✦ Completely unobtrusive✦ Uses CSS to layer functionality✦ Easy to separate behavior✦ Quick, terse, syntax

Page 4: Building Interactive Prototypes with jQuery

The Focus of jQuery

Find Some Elements

$(“div”).addClass(“special”);Do something with them{ {

jQuery Object

Page 5: Building Interactive Prototypes with jQuery

The jQuery Object✦ Commonly named ‘$’✦ Also named ‘jQuery’✦ Completely valid:

✦ jQuery(“div”).addClass(“special”);

Page 6: Building Interactive Prototypes with jQuery

Find Some Elements...✦ Full CSS Selector 1-3 Support✦ Basic XPath✦ Better CSS Selector support than most

browsers

Page 7: Building Interactive Prototypes with jQuery

$(“div”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 8: Building Interactive Prototypes with jQuery

$(“#body”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 9: Building Interactive Prototypes with jQuery

$(“div#body”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 10: Building Interactive Prototypes with jQuery

$(“div.contents p”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 11: Building Interactive Prototypes with jQuery

$(“div > div”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 12: Building Interactive Prototypes with jQuery

$(“div:has(div)”)<div id=”body”> <h2>Some Header</h2> <div class=”contents”> <p>...</p> <p>...</p> </div></div>

Page 13: Building Interactive Prototypes with jQuery

✦ DOM Manipulation (append, prepend, remove)

✦ Events (click, hover, toggle)

✦ Effects (hide, show, slideDown, fadeOut)

✦ AJAX (load, get, post)

Do something with them

Page 14: Building Interactive Prototypes with jQuery

DOM Manipulation✦ $(“a[target]”)

.append(“ (Opens in New Window)”);✦ $(“#body”).css({

border: “1px solid green”, height: “40px”});

Page 15: Building Interactive Prototypes with jQuery

Events✦ $(“form”).submit(function(){

if ( $(“input#name”).val() == “” ) $(“span.help”).show();});

✦ $(“a#open”).click(function(){ $(“#menu”).show(); return false;});

Page 16: Building Interactive Prototypes with jQuery

Animations✦ $(“#menu”).slideDown(“slow”);✦ Individual properties:$(“div”).animate({ fontWeight: “2em”, width: “+=20%”, color: “green” // via plugin});

✦ Callbacks:$(“div”).hide(500, function(){ // $(this) is an individual <div> element $(this).show(500);});

Page 17: Building Interactive Prototypes with jQuery

Ajax✦ $(“#body”).load(“sample.html div > h1”);

✦ Before:<div id=”body”></div>

✦ After:<div id=”body”><h1>Hello, world!</h1></div>

✦ $.getJSON(“test.json”, function(js){ for ( var name in js ) $(“ul”).append(“<li>” + name + “</li>”);});

Page 18: Building Interactive Prototypes with jQuery

✦ You can have multiple actions against a single set of elements

✦ $(“div”).hide();

✦ $(“div”).hide().css(“color”,”blue”);

✦ $(“div”).hide().css(“color”,”blue”).slideDown();

Chaining

Page 19: Building Interactive Prototypes with jQuery

Chaining (cont.)✦ $(“ul.open”) // [ ul, ul, ul ]

.children(“li”) // [ li, li, li ] .addClass(“open”) // [ li, li, li] .end() // [ ul, ul, ul ] .find(“a”) // [ a, a, a ] .click(function(){ $(this).next().toggle(); return false; }) // [ a, a, a ] .end(); // [ ul, ul, ul ]

Page 20: Building Interactive Prototypes with jQuery

✦ Fully documented✦ Great community✦ Tons of plugins✦ Small size (14kb)✦ Everything works in IE 6+, Firefox,

Safari 2+, and Opera 9+

Why jQuery?

Page 21: Building Interactive Prototypes with jQuery

jQuery Plugins✦ Extend the jQuery system✦ Add on extra methods:$(“div”).hideRemove();

✦ Trivial to implement:jQuery.fn.hideRemove = function(speed){ return this.hide(speed, function(){ jQuery(this).remove(); });};

Page 22: Building Interactive Prototypes with jQuery

Plugins✦ Drag and Drop✦ Sortables✦ Modal Dialogs✦ Tabbed Navigation✦ Sortable Tables✦ And hundreds more...

Page 23: Building Interactive Prototypes with jQuery

Prototyping Demos

Page 24: Building Interactive Prototypes with jQuery

Accordion Menuhttp://jquery.com/files/apple/

http://jquery.com/files/apple/done.html

Page 25: Building Interactive Prototypes with jQuery

Social Networkinghttp://jquery.com/files/social/

http://jquery.com/files/social/done.php

Page 26: Building Interactive Prototypes with jQuery

Todo Listhttp://jquery.com/files/todo/

http://jquery.com/files/todo/done.php

Page 27: Building Interactive Prototypes with jQuery

Who uses jQuery?✦ Google✦ IBM✦ NBC✦ Amazon✦ Wordpress✦ Digg✦ many others...

Page 28: Building Interactive Prototypes with jQuery

✦ Very active mailing list✦ 108+ Posts/Day✦ 4000+ Members

✦ Technorati: Dozens of blog posts per day

Community

Page 29: Building Interactive Prototypes with jQuery

Books✦ 2 Books Released:

✦ Learning jQuery (Packt)✦ jQuery Reference (Packt)

✦ 1 Book in Progress:✦ jQuery in Action (Manning)

Page 30: Building Interactive Prototypes with jQuery

jQuery UI✦ Full set of themed widgets and

components✦ Drag & Drop✦ Tabs✦ Table✦ Modal Dialog✦ etc.

Page 31: Building Interactive Prototypes with jQuery

jquery.comdocs.jquery.com - jquery.com/plugins

More:ui.jquery.com

visualjquery.comlearningjquery.com