SlideShare a Scribd company logo
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Why not use Prototype, jQuery, etc.
Lots of code = long time to download
Caching on mobile devices is not so great
Most code is for browsers that you don’t need to
support (IE6 doesn’t run on an iPhone!)
Natively supported features are duplicated,
for example JSON support and animations
Goals for a mobile
JavaScript framework
Very small codebase
Easy to use API for common DOM tasks
Easy to extend & customize
No need to deal with browser cruft (IE6, etc)
Inlineable
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
http://en.wikipedia.org/wiki/Aerogel
0.002kg
2.5kg
Size: ~2K
jQuery-compatible API
Uses mobile WebKit features as much as possible
Easily replaceable with larger libs if your app grows
Open source (MIT license)
$('p').html('test').css('color:red');
get(): return array of all elements found
get(0): return first element found
each(callback): iterate over array of all elements found
index('selector'): return an integer indicating the position of 'selector' in array of all elements found
first(): remove all but the first element from the list of found elements
find('selector'): find all children/grandchildren that match the given selector
closest('selector'): traverses the DOM upwards to find the first matching element
next(): next siblings
prev(): previous siblings
is('selector'): returns true/false if first element matches the selector
remove(): remove element
html('new html'): set the contents of the element(s)
append, prepend, before, after: like html, but add html to element contents (or before/after)
html(): get first elements .innerHTML
show(): forces elements to be displayed (only works correctly for block elements right now)
hide(): removes a elements from layout
offset(): get object with top: left: width: height: properties (in px)
height(): get first elements height in px
width(): get first elements width in px
attr('attribute'): get element attribute
attr('attribute', 'value'): set element attribute
css('css property', 'value'): set a CSS property
css({ property1: value1, property2: value2 }): set multiple CSS properties
css('css property'): get this CSS property of the first element
addClass('classname'): adds a CSS class name
removeClass('classname'): removes a CSS class name
hasClass('classname'): returns true of first element has a classname set
bind(type, function): add an event listener (see below)
delegate(selector, type, function): add an event listener w/ event delegation (see below)
live(type, function): add an event listener that listens to the selector for current and future elements
trigger(type): triggers an event
get(): return array of all elements found
get(0): return first element found
each(callback): iterate over array of all elements found
index('selector'): return an integer indicating the position of 'selector' in array of all elements found
first(): remove all but the first element from the list of found elements
find('selector'): find all children/grandchildren that match the given selector
closest('selector'): traverses the DOM upwards to find the first matching element
next(): next siblings
prev(): previous siblings
is('selector'): returns true/false if first element matches the selector
remove(): remove element
html('new html'): set the contents of the element(s)
append, prepend, before, after: like html, but add html to element contents (or before/after)
html(): get first elements .innerHTML
show(): forces elements to be displayed (only works correctly for block elements right now)
hide(): removes a elements from layout
offset(): get object with top: left: width: height: properties (in px)
height(): get first elements height in px
width(): get first elements width in px
attr('attribute'): get element attribute
attr('attribute', 'value'): set element attribute
css('css property', 'value'): set a CSS property
css({ property1: value1, property2: value2 }): set multiple CSS properties
css('css property'): get this CSS property of the first element
addClass('classname'): adds a CSS class name
removeClass('classname'): removes a CSS class name
hasClass('classname'): returns true of first element has a classname set
bind(type, function): add an event listener (see below)
delegate(selector, type, function): add an event listener w/ event delegation (see below)
live(type, function): add an event listener that listens to the selector for current and future elements
trigger(type): triggers an event
Basically, everything
$.get(url, callback)
$.post(url, callback)
$.getJSON(url, callback)
$('selector').load('url'[, callback]);
$('selector').load('url #fragment-selector'[, callback]);
Ajax
$('some selector').tap(function(){ ... });
$('some selector').doubleTap(function(){ ... });
$('some selector').swipe(function(){ ... });
Tap & Swipe
Uncompresed Minified Minified & Gzipped
0K
50K
100K
150K
200K
180K
160K
9K
78K 87K
6K
27K
27K
2K
Uncompresed Minified Minified & Gzipped
0K
50K
100K
150K
200K
180K
160K
9K
78K 87K
6K
27K
27K
2K
Uncompresed Minified Minified & Gzipped
jQuery
0K
50K
100K
150K
200K
180K
160K
9K
78K 87K
6K
27K
27K
2K
Uncompresed Minified Minified & Gzipped
jQuery
Prototype
0K
50K
100K
150K
200K
180K
160K
9K
78K 87K
6K
27K
27K
2K
Uncompresed Minified Minified & Gzipped
jQuery
Prototype
Zepto.js
Uncompresed Minified Minified & Gzipped
8.788K
5.816K
2.368K
Uncompresed Minified Minified & Gzipped
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
$.css = function(style){
this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this;
}
Core implementation (simplified)
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
$.css = function(style){
this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this;
}
$(‘some CSS selector’)
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
$.css = function(style){
this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this;
}
returns a zepto.js object
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
$.css = function(style){
this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this;
}
html(‘new html’) sets the contents
of one or more elements
css(‘style’) sets the style of
one or more elements
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
$.css = function(style){
this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this;
}
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
How $() works
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
select elements on the page as per
the user-specified CSS selector
$('p')
How $() works
var $ = function(selector){
return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)),
anim: $.anim, css: $.css, html: $.html };
}
return a “zepto” object
{
dom: [/* element 1, element 2, element 3, etc.*/],
css: $.css, html: $.html
}
How $() works
(cc) http://www.flickr.com/photos/gloson/4594527045
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
How a chainable function works
this.dom refers to the nodes
selected by the call to $
How a chainable function works
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
forEach iterates over all the nodes
(nodelist was converted to an array)
How a chainable function works
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
set the contents of the node to
some specificed html
How a chainable function works
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
return the “zepto” object
for chaining
How a chainable function works
$.html = function(html){
this.dom.forEach(function(el){ el.innerHTML = html }); return this;
}
Inlining FTW
<!DOCTYPE html>
<html>
<head>
<title>Zepto.js</title>
<script>
// stick all JS stuff in here
</script>
</head>
<body>
<p>
Blah
</p>
<p>
Blub
</p>
<script>
$('p').html('test').css('color:red');
</script>
</body>
</html>
function $$(el, selector){
return slice.call(el.querySelectorAll(selector))
}
function compact(array){
return array.filter(function(el){
return el !== void 0 && el !== null
})
}
function $(_, context){
if(context !== void 0) return $(context).find(_);
function fn(_){ return fn.dom.forEach(_), fn }
fn.dom = compact((typeof _ == 'function' && 'dom' in _) ?
_.dom : (_ instanceof Array ? _ :
(_ instanceof Element ? [_] :
$$(d, fn.selector = _))));
$.extend(fn, $.fn);
return fn;
}
Only targets mobile browsers
Minimalist, jQuery-ish framework
Use WebKit features as much as possible
Can be “upgraded”
Inlineable
http://zeptojs.com/

More Related Content

What's hot (20)

PDF
History of jQuery
jeresig
 
PPT
jQuery
Mostafa Bayomi
 
PPTX
jQuery PPT
Dominic Arrojado
 
PDF
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
PDF
jQuery Essentials
Bedis ElAchèche
 
PDF
Prototype & jQuery
Remy Sharp
 
PPTX
jQuery
Dileep Mishra
 
PPTX
jQuery
Vishwa Mohan
 
ODP
Drupal Best Practices
manugoel2003
 
PDF
Testing Web Applications with GEB
Howard Lewis Ship
 
PPTX
jQuery Presentation
Rod Johnson
 
PDF
jQuery for beginners
Siva Arunachalam
 
PDF
HTML5 JavaScript APIs
Remy Sharp
 
PDF
The jQuery Divide
Rebecca Murphey
 
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
PPT
A Short Introduction To jQuery
Sudar Muthu
 
PDF
Introduction to jQuery
Zeeshan Khan
 
PDF
jQuery Features to Avoid
dmethvin
 
PPTX
Introduction to jQuery
Gunjan Kumar
 
History of jQuery
jeresig
 
jQuery PPT
Dominic Arrojado
 
Learning jQuery made exciting in an interactive session by one of our team me...
Thinqloud
 
jQuery Essentials
Bedis ElAchèche
 
Prototype & jQuery
Remy Sharp
 
jQuery
Vishwa Mohan
 
Drupal Best Practices
manugoel2003
 
Testing Web Applications with GEB
Howard Lewis Ship
 
jQuery Presentation
Rod Johnson
 
jQuery for beginners
Siva Arunachalam
 
HTML5 JavaScript APIs
Remy Sharp
 
The jQuery Divide
Rebecca Murphey
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
A Short Introduction To jQuery
Sudar Muthu
 
Introduction to jQuery
Zeeshan Khan
 
jQuery Features to Avoid
dmethvin
 
Introduction to jQuery
Gunjan Kumar
 

Viewers also liked (20)

PPT
Service!
한섭 심
 
PDF
Boe February 10 2009 Agenda
Hoboken Resistance
 
PDF
4 p xopoo
Telmen telmen
 
PDF
Escala digital
lucas fortes
 
PPTX
KV Menu 6-25-2015
knoxvillevapor
 
PPT
Ppt32
lotscarf
 
PDF
The Beauty Of The White Mountains In New Hampshire
Tiffany Kate Roth
 
PDF
Published Sox9 Paper!
Tim Rutkowski
 
PDF
Optical flares installation
galamo11
 
PDF
Hands Down Teacher's Worksheet
EOI GAMES
 
PPTX
Examples of successful simulation in the development cycly of Kolektor Etra
SIMTEC Software and Services
 
DOC
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
Alessandra Vidal
 
PPT
Ratib al haddad(revised-2011)
Zhulkeflee Ismail
 
PPT
Fgcfg
guest2a1b1de5
 
PPTX
Simple present for schedules
Nadia Espinosa
 
PDF
X2 t08 04 inequality techniques (2012)
Nigel Simmons
 
PDF
INGLES A1
Johanna Parra Avila
 
PPT
Strategic Management Ch05
Chuong Nguyen
 
PPTX
Chinese stone lions
Karen Weinstein
 
PPS
Easy but Difficult
KHMEREMPIRE PA
 
Service!
한섭 심
 
Boe February 10 2009 Agenda
Hoboken Resistance
 
4 p xopoo
Telmen telmen
 
Escala digital
lucas fortes
 
KV Menu 6-25-2015
knoxvillevapor
 
Ppt32
lotscarf
 
The Beauty Of The White Mountains In New Hampshire
Tiffany Kate Roth
 
Published Sox9 Paper!
Tim Rutkowski
 
Optical flares installation
galamo11
 
Hands Down Teacher's Worksheet
EOI GAMES
 
Examples of successful simulation in the development cycly of Kolektor Etra
SIMTEC Software and Services
 
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
Alessandra Vidal
 
Ratib al haddad(revised-2011)
Zhulkeflee Ismail
 
Simple present for schedules
Nadia Espinosa
 
X2 t08 04 inequality techniques (2012)
Nigel Simmons
 
Strategic Management Ch05
Chuong Nguyen
 
Chinese stone lions
Karen Weinstein
 
Easy but Difficult
KHMEREMPIRE PA
 
Ad

Similar to Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K (20)

KEY
An in-depth look at jQuery
Paul Bakaus
 
PDF
jQuery
Ivano Malavolta
 
PDF
jQuery (MeshU)
jeresig
 
PDF
jQuery (BostonPHP)
jeresig
 
PPT
JQuery: Introduction
Amit Kumar Singh
 
PPT
J Query Public
pradeepsilamkoti
 
PPT
Jquery presentation
guest5d87aa6
 
PPTX
Iniciando com jquery
Danilo Sousa
 
KEY
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
PPTX
Introduction to Jquery
Ahmed Elharouny
 
PDF
jQuery (DrupalCamp Toronto)
jeresig
 
KEY
jQuery Anti-Patterns for Performance
András Kovács
 
PDF
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
PPTX
Getting Started with jQuery
Akshay Mathur
 
PPTX
Unobtrusive javascript with jQuery
Angel Ruiz
 
PDF
Jquery in-15-minutes1421
palsingh26
 
PDF
jQuery Internals + Cool Stuff
jeresig
 
PPT
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
PPT
Eugene Andruszczenko: jQuery
Refresh Events
 
An in-depth look at jQuery
Paul Bakaus
 
jQuery (MeshU)
jeresig
 
jQuery (BostonPHP)
jeresig
 
JQuery: Introduction
Amit Kumar Singh
 
J Query Public
pradeepsilamkoti
 
Jquery presentation
guest5d87aa6
 
Iniciando com jquery
Danilo Sousa
 
jQuery Anti-Patterns for Performance & Compression
Paul Irish
 
Introduction to Jquery
Ahmed Elharouny
 
jQuery (DrupalCamp Toronto)
jeresig
 
jQuery Anti-Patterns for Performance
András Kovács
 
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
Getting Started with jQuery
Akshay Mathur
 
Unobtrusive javascript with jQuery
Angel Ruiz
 
Jquery in-15-minutes1421
palsingh26
 
jQuery Internals + Cool Stuff
jeresig
 
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
Eugene Andruszczenko: jQuery
Refresh Events
 
Ad

More from Thomas Fuchs (9)

PDF
Zepto and the rise of the JavaScript Micro-Frameworks
Thomas Fuchs
 
PDF
I Can't Believe It's Not Flash
Thomas Fuchs
 
PDF
Prototype & Scriptaculous
Thomas Fuchs
 
PDF
Extreme JavaScript Performance
Thomas Fuchs
 
PDF
Textorize
Thomas Fuchs
 
PDF
Adventures In JavaScript Testing
Thomas Fuchs
 
PDF
Ruby On Rails Introduction
Thomas Fuchs
 
KEY
Twistori Tech
Thomas Fuchs
 
PDF
Rich and Snappy Apps (No Scaling Required)
Thomas Fuchs
 
Zepto and the rise of the JavaScript Micro-Frameworks
Thomas Fuchs
 
I Can't Believe It's Not Flash
Thomas Fuchs
 
Prototype & Scriptaculous
Thomas Fuchs
 
Extreme JavaScript Performance
Thomas Fuchs
 
Textorize
Thomas Fuchs
 
Adventures In JavaScript Testing
Thomas Fuchs
 
Ruby On Rails Introduction
Thomas Fuchs
 
Twistori Tech
Thomas Fuchs
 
Rich and Snappy Apps (No Scaling Required)
Thomas Fuchs
 

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Horarios de distribución de agua en julio
pegazohn1978
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 

Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K

  • 4. Why not use Prototype, jQuery, etc. Lots of code = long time to download Caching on mobile devices is not so great Most code is for browsers that you don’t need to support (IE6 doesn’t run on an iPhone!) Natively supported features are duplicated, for example JSON support and animations
  • 5. Goals for a mobile JavaScript framework Very small codebase Easy to use API for common DOM tasks Easy to extend & customize No need to deal with browser cruft (IE6, etc) Inlineable
  • 10. Size: ~2K jQuery-compatible API Uses mobile WebKit features as much as possible Easily replaceable with larger libs if your app grows Open source (MIT license)
  • 12. get(): return array of all elements found get(0): return first element found each(callback): iterate over array of all elements found index('selector'): return an integer indicating the position of 'selector' in array of all elements found first(): remove all but the first element from the list of found elements find('selector'): find all children/grandchildren that match the given selector closest('selector'): traverses the DOM upwards to find the first matching element next(): next siblings prev(): previous siblings is('selector'): returns true/false if first element matches the selector remove(): remove element html('new html'): set the contents of the element(s) append, prepend, before, after: like html, but add html to element contents (or before/after) html(): get first elements .innerHTML show(): forces elements to be displayed (only works correctly for block elements right now) hide(): removes a elements from layout offset(): get object with top: left: width: height: properties (in px) height(): get first elements height in px width(): get first elements width in px attr('attribute'): get element attribute attr('attribute', 'value'): set element attribute css('css property', 'value'): set a CSS property css({ property1: value1, property2: value2 }): set multiple CSS properties css('css property'): get this CSS property of the first element addClass('classname'): adds a CSS class name removeClass('classname'): removes a CSS class name hasClass('classname'): returns true of first element has a classname set bind(type, function): add an event listener (see below) delegate(selector, type, function): add an event listener w/ event delegation (see below) live(type, function): add an event listener that listens to the selector for current and future elements trigger(type): triggers an event
  • 13. get(): return array of all elements found get(0): return first element found each(callback): iterate over array of all elements found index('selector'): return an integer indicating the position of 'selector' in array of all elements found first(): remove all but the first element from the list of found elements find('selector'): find all children/grandchildren that match the given selector closest('selector'): traverses the DOM upwards to find the first matching element next(): next siblings prev(): previous siblings is('selector'): returns true/false if first element matches the selector remove(): remove element html('new html'): set the contents of the element(s) append, prepend, before, after: like html, but add html to element contents (or before/after) html(): get first elements .innerHTML show(): forces elements to be displayed (only works correctly for block elements right now) hide(): removes a elements from layout offset(): get object with top: left: width: height: properties (in px) height(): get first elements height in px width(): get first elements width in px attr('attribute'): get element attribute attr('attribute', 'value'): set element attribute css('css property', 'value'): set a CSS property css({ property1: value1, property2: value2 }): set multiple CSS properties css('css property'): get this CSS property of the first element addClass('classname'): adds a CSS class name removeClass('classname'): removes a CSS class name hasClass('classname'): returns true of first element has a classname set bind(type, function): add an event listener (see below) delegate(selector, type, function): add an event listener w/ event delegation (see below) live(type, function): add an event listener that listens to the selector for current and future elements trigger(type): triggers an event Basically, everything
  • 14. $.get(url, callback) $.post(url, callback) $.getJSON(url, callback) $('selector').load('url'[, callback]); $('selector').load('url #fragment-selector'[, callback]); Ajax
  • 15. $('some selector').tap(function(){ ... }); $('some selector').doubleTap(function(){ ... }); $('some selector').swipe(function(){ ... }); Tap & Swipe
  • 23. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } $.css = function(style){ this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this; } Core implementation (simplified)
  • 24. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } $.css = function(style){ this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this; } $(‘some CSS selector’)
  • 25. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } $.css = function(style){ this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this; } returns a zepto.js object
  • 26. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } $.css = function(style){ this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this; } html(‘new html’) sets the contents of one or more elements
  • 27. css(‘style’) sets the style of one or more elements var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } $.css = function(style){ this.dom.forEach(function(el){ el.style.cssText += ';'+style }); return this; }
  • 28. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } How $() works
  • 29. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } select elements on the page as per the user-specified CSS selector $('p') How $() works
  • 30. var $ = function(selector){ return { dom: Array.prototype.slice.apply(document.querySelectorAll(selector)), anim: $.anim, css: $.css, html: $.html }; } return a “zepto” object { dom: [/* element 1, element 2, element 3, etc.*/], css: $.css, html: $.html } How $() works
  • 32. $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; } How a chainable function works
  • 33. this.dom refers to the nodes selected by the call to $ How a chainable function works $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; }
  • 34. forEach iterates over all the nodes (nodelist was converted to an array) How a chainable function works $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; }
  • 35. set the contents of the node to some specificed html How a chainable function works $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; }
  • 36. return the “zepto” object for chaining How a chainable function works $.html = function(html){ this.dom.forEach(function(el){ el.innerHTML = html }); return this; }
  • 37. Inlining FTW <!DOCTYPE html> <html> <head> <title>Zepto.js</title> <script> // stick all JS stuff in here </script> </head> <body> <p> Blah </p> <p> Blub </p> <script> $('p').html('test').css('color:red'); </script> </body> </html>
  • 38. function $$(el, selector){ return slice.call(el.querySelectorAll(selector)) } function compact(array){ return array.filter(function(el){ return el !== void 0 && el !== null }) } function $(_, context){ if(context !== void 0) return $(context).find(_); function fn(_){ return fn.dom.forEach(_), fn } fn.dom = compact((typeof _ == 'function' && 'dom' in _) ? _.dom : (_ instanceof Array ? _ : (_ instanceof Element ? [_] : $$(d, fn.selector = _)))); $.extend(fn, $.fn); return fn; }
  • 39. Only targets mobile browsers Minimalist, jQuery-ish framework Use WebKit features as much as possible Can be “upgraded” Inlineable http://zeptojs.com/