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

More Related Content

What's hot (20)

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

Viewers also liked (20)

Boe February 10 2009 Agenda
Boe February 10 2009 AgendaBoe February 10 2009 Agenda
Boe February 10 2009 Agenda
Hoboken Resistance
 
4 p xopoo
4 p xopoo4 p xopoo
4 p xopoo
Telmen telmen
 
Escala digital
Escala digitalEscala digital
Escala digital
lucas fortes
 
KV Menu 6-25-2015
KV Menu 6-25-2015KV Menu 6-25-2015
KV Menu 6-25-2015
knoxvillevapor
 
Ppt32
Ppt32Ppt32
Ppt32
lotscarf
 
The Beauty Of The White Mountains In New Hampshire
The Beauty Of The White Mountains In New HampshireThe Beauty Of The White Mountains In New Hampshire
The Beauty Of The White Mountains In New Hampshire
Tiffany Kate Roth
 
Published Sox9 Paper!
Published Sox9 Paper!Published Sox9 Paper!
Published Sox9 Paper!
Tim Rutkowski
 
Optical flares installation
Optical flares installationOptical flares installation
Optical flares installation
galamo11
 
Hands Down Teacher's Worksheet
Hands Down Teacher's WorksheetHands Down Teacher's Worksheet
Hands Down Teacher's Worksheet
EOI GAMES
 
Examples of successful simulation in the development cycly of Kolektor Etra
Examples of successful simulation in the development cycly of Kolektor EtraExamples of successful simulation in the development cycly of Kolektor Etra
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
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
Alessandra Vidal
 
Ratib al haddad(revised-2011)
Ratib al haddad(revised-2011)Ratib al haddad(revised-2011)
Ratib al haddad(revised-2011)
Zhulkeflee Ismail
 
Simple present for schedules
Simple present for schedulesSimple present for schedules
Simple present for schedules
Nadia Espinosa
 
X2 t08 04 inequality techniques (2012)
X2 t08 04 inequality techniques (2012)X2 t08 04 inequality techniques (2012)
X2 t08 04 inequality techniques (2012)
Nigel Simmons
 
INGLES A1
INGLES A1INGLES A1
INGLES A1
Johanna Parra Avila
 
Strategic Management Ch05
Strategic Management Ch05Strategic Management Ch05
Strategic Management Ch05
Chuong Nguyen
 
Chinese stone lions
Chinese stone lionsChinese stone lions
Chinese stone lions
Karen Weinstein
 
Easy but Difficult
Easy but DifficultEasy but Difficult
Easy but Difficult
KHMEREMPIRE PA
 
The Beauty Of The White Mountains In New Hampshire
The Beauty Of The White Mountains In New HampshireThe Beauty Of The White Mountains In New Hampshire
The Beauty Of The White Mountains In New Hampshire
Tiffany Kate Roth
 
Published Sox9 Paper!
Published Sox9 Paper!Published Sox9 Paper!
Published Sox9 Paper!
Tim Rutkowski
 
Optical flares installation
Optical flares installationOptical flares installation
Optical flares installation
galamo11
 
Hands Down Teacher's Worksheet
Hands Down Teacher's WorksheetHands Down Teacher's Worksheet
Hands Down Teacher's Worksheet
EOI GAMES
 
Examples of successful simulation in the development cycly of Kolektor Etra
Examples of successful simulation in the development cycly of Kolektor EtraExamples of successful simulation in the development cycly of Kolektor Etra
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
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
6847575 a-saga-dos-foxworth-4-sementes-do-passado-virginia-c-andrews
Alessandra Vidal
 
Ratib al haddad(revised-2011)
Ratib al haddad(revised-2011)Ratib al haddad(revised-2011)
Ratib al haddad(revised-2011)
Zhulkeflee Ismail
 
Simple present for schedules
Simple present for schedulesSimple present for schedules
Simple present for schedules
Nadia Espinosa
 
X2 t08 04 inequality techniques (2012)
X2 t08 04 inequality techniques (2012)X2 t08 04 inequality techniques (2012)
X2 t08 04 inequality techniques (2012)
Nigel Simmons
 
Strategic Management Ch05
Strategic Management Ch05Strategic Management Ch05
Strategic Management Ch05
Chuong Nguyen
 
Ad

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

Javascript And J Query
Javascript And J QueryJavascript And J Query
Javascript And J Query
itsarsalan
 
Prototype Framework
Prototype FrameworkPrototype Framework
Prototype Framework
Julie Iskander
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
kolkatageeks
 
jQuery, CSS3 and ColdFusion
jQuery, CSS3 and ColdFusionjQuery, CSS3 and ColdFusion
jQuery, CSS3 and ColdFusion
Denard Springle IV
 
J Query
J QueryJ Query
J Query
Compare Infobase Limited
 
Scala on Your Phone
Scala on Your PhoneScala on Your Phone
Scala on Your Phone
Michael Galpin
 
Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1
saydin_soft
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
Daniel Knell
 
J query training
J query trainingJ query training
J query training
FIS - Fidelity Information Services
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
Pradeep Saraswathi
 
jQuery
jQueryjQuery
jQuery
Andrew Homeyer
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
Allegient
 
jQuery
jQueryjQuery
jQuery
Ivano Malavolta
 
JQuery
JQueryJQuery
JQuery
SelmanJagxhiu
 
How te bring common UI patterns to ADF
How te bring common UI patterns to ADFHow te bring common UI patterns to ADF
How te bring common UI patterns to ADF
Getting value from IoT, Integration and Data Analytics
 
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
JavaScript JQUERY AJAX
JavaScript JQUERY AJAXJavaScript JQUERY AJAX
JavaScript JQUERY AJAX
Makarand Bhatambarekar
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
ghnash
 
Persisting Data on SQLite using Room
Persisting Data on SQLite using RoomPersisting Data on SQLite using Room
Persisting Data on SQLite using Room
Nelson Glauber Leal
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
orestJump
 
Ad

More from Thomas Fuchs (9)

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

Recently uploaded (20)

Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 

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/