SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
Introduction to
JavaScript #3
@danielknell
Friday, 11 October 13
Recap
var element = document.getElementById('puzzle');
var elements = document.getElementsByTagName('div');
var elements = document.getElementsByClassName('submit'); // not IE < 9
var element = document.querySelector('#reload .submit'); // not IE < 8
var elements = document.querySelectorAll('#reload .submit'); // not IE < 8
element.getElementById('child');
element.getElementsByTagName('span');
element.getElementsByClassName('foo');
element.querySelector('#reload .submit'); // not IE < 8
element.querySelectorAll('#reload .submit'); // not IE < 8a
Friday, 11 October 13
Recap
var children = element.children;
var parent = element.parentNode;
Friday, 11 October 13
Recap
var parent = document.getElementById('foobar');
var element = document.createElement('div');
parent.appendChild(element);
parent.insertBefore(element, someOtherElement);
parent.removeChild(element);
element.parentNode.removeChild(element);
element.textContent = 'hello world';
Friday, 11 October 13
Recap
var ol = document.createElement('ol')
, el
;
document.getElementById('output').appendChild(ol);
for (var i = 1; i <= 100; ++i) {
el = document.createElement('li');
if (i % 3 === 0 && i % 5 === 0) {
el.textContent = 'FizzBuzz';
}
else if (i % 3 === 0) {
el.textContent = 'Fizz';
}
else if (i % 5 === 0) {
el.textContent = 'Buzz';
}
else {
el.textContent = i;
}
ol.appendChild(el);
}
Friday, 11 October 13
Recap
var el = document.getElementById('output');
el.style.backgroundImage = "url('bg.jpg')";
el.style.fontWeight = ‘bold’;
el.style.color = ‘red’;
window.getComputedStyle(el).backgroundImage; // not IE <
9
el.currentStyle.backgroundImage; // IE < 9
Friday, 11 October 13
Recap
var items = ol.children
;
for (var i = 0; i < items.length; ++i) {
el = items[i];
if (
el.textContent === 'FizzBuzz' ||
el.textContent === 'Fizz' ||
el.textContent === 'Buzz'
) {
el.style.fontWeight = 'bold';
}
if (i % 2 === 0) {
el.style.color = 'red';
}
}
Friday, 11 October 13
http://artsn.co/js-tpl
Friday, 11 October 13
Nested Loops
var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]]
;
for (var x = 0; x < data.length; ++x) {
for (var y = 0; y < data[x].length; ++y) {
console.log(data[x][y]);
}
}
Friday, 11 October 13
Element Property
var el = document.getElementById('output');
el.id;
el.className;
Friday, 11 October 13
Math is Hard
Friday, 11 October 13
Math is Hard
.item {
display: block;
position: absolute;
line-height: 40px;
text-align: center;
}
#output {
position: relative;
overflow: hidden;
border: 1px solid #000;
margin: 20px auto;
}
Friday, 11 October 13
Math is Hard
var x
, y
, i = 0
, pieceWidth = 40
, pieceHeight = 40
, el = document.getElementById('output')
, size = 11
;
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
child = document.createElement('div');
child.style.width = pieceWidth + 'px';
child.style.height = pieceHeight + 'px';
child.style.left = (x * pieceWidth) + 'px';
child.style.top = (y * pieceHeight) + 'px';
child.className = 'item'
// to be continued...
Friday, 11 October 13
Math is Hard
if (x === 0 || y === 0) {
child.style.backgroundColor = '#eee';
}
if (x === 0 && y === 0) {
child.innerHTML = '+'
}
else {
child.textContent = x + y;
}
el.appendChild(child);
i++;
}
}
el.style.width = pieceWidth * size + 'px';
el.style.height = pieceHeight * size + 'px';
Friday, 11 October 13
Element Attribute
var el = document.getElementById('output');
el.setAttribute('lang', 'en');
el.getAttribute('lang');
// <div id="output" lang="en"></div>
el.setAttribute('data-foo', 'foo');
el.getAttribute('data-foo');
// <div id="output" data-foo="foo"></div>
Friday, 11 October 13
Slide Puzzle
Friday, 11 October 13
Slide Puzzle
var x
, y
, i = 0
, bgX
, bgY
, size = 5
, pieceWidth = Math.floor(612 / size)
, pieceHeight = Math.floor(612 / size)
, el = document.getElementById('output')
;
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
if (x == size - 1 && y == size - 1) {
continue;
}
bgX = pieceWidth * x;
bgY = pieceHeight * y;
child = document.createElement('div');
// to be continued...
Friday, 11 October 13
Slide Puzzle
child.style.backgroundPosition = "-" + bgX + 'px -' + bgY
+ 'px';
child.style.width = pieceWidth + 'px';
child.style.height = pieceHeight + 'px';
child.style.left = (x * pieceWidth) + 'px';
child.style.top = (y * pieceHeight) + 'px';
child.className = 'piece'
child.setAttribute('data-x', x);
child.setAttribute('data-y', y);
el.appendChild(child);
}
}
el.style.width = pieceWidth * size + 'px';
el.style.height = pieceHeight * size + 'px';
Friday, 11 October 13
Events
Friday, 11 October 13
Events
var el = document.getElementById('output');
el.addEventListener('click', callback, false); // not IE < 9
el.attachEvent('onclick', callback); // IE < 9
Friday, 11 October 13
Events
var el = document.getElementById('output');
function callback(e) {
alert('hello world');
e.preventDefault();
}
el.addEventListener('click', callback, false);
Friday, 11 October 13
Greeter
Friday, 11 October 13
Greeter
<div id="output">
<form id="form">
<label for="input">Name</label>
<input id="input" type="text">
<button type="submit">Greet</button>
</form>
</div>
Friday, 11 October 13
Moar Math!
Friday, 11 October 13
Moar Math!
Math.round(0.5); // 1
Math.floor(0.9); // 0
Math.ceil(0.1); // 1
Math.abs(-1); // 1
Math.sqrt(9); // 3
Math.sin(1); // 0.8414709848078965
Math.cos(1); // 0.5403023058681398
Math.tan(1); // 1.5574077246549023
Math.asin(1); // 1.5707963267948966
Math.acos(1); // 0
Math.atan(1); // 0.7853981633974483
Math.min(1, 5); // 1
Math.max(1, 5); // 5
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Friday, 11 October 13
Slide Puzzle
.piece {
background-image: url('../img/image.jpg');
display: block;
position: absolute;
transition: top 1s, left 1s;
}
#output {
position: relative;
overflow: hidden;
border: 1px solid #000;
margin: 20px auto;
}
Friday, 11 October 13
Slide Puzzle
Friday, 11 October 13
Thats All Folks
email: contact@danielknell.co.uk
twitter: @danielknell
website: http://danielknell.co.uk/
Friday, 11 October 13

Más contenido relacionado

La actualidad más candente

Kick start with j query
Kick start with j queryKick start with j query
Kick start with j query
Md. Ziaul Haq
 
Full compile invalid obje pl/sql
Full compile invalid obje pl/sqlFull compile invalid obje pl/sql
Full compile invalid obje pl/sql
Anar Godjaev
 

La actualidad más candente (7)

Events - Part 2
Events - Part 2Events - Part 2
Events - Part 2
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Kick start with j query
Kick start with j queryKick start with j query
Kick start with j query
 
An Introduction to Jquery
An Introduction to JqueryAn Introduction to Jquery
An Introduction to Jquery
 
BVJS
BVJSBVJS
BVJS
 
Full compile invalid obje pl/sql
Full compile invalid obje pl/sqlFull compile invalid obje pl/sql
Full compile invalid obje pl/sql
 
Practical Event Sourcing
Practical Event SourcingPractical Event Sourcing
Practical Event Sourcing
 

Destacado

An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
Event Handler
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup EmpireThe Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2The big bang theory - UNIT 2
The big bang theory - UNIT 2
lm092068
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week TwoIntroduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
Event Handler
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
Event Handler
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
Event Handler
 
Big Bang Theorychandler
Big Bang TheorychandlerBig Bang Theorychandler
Big Bang Theorychandler
guest008d7bd
 

Destacado (20)

An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup EmpireThe Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2The big bang theory - UNIT 2
The big bang theory - UNIT 2
 
Java Script
Java ScriptJava Script
Java Script
 
The big bang theory of social recruiting
The big bang theory of social recruitingThe big bang theory of social recruiting
The big bang theory of social recruiting
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week TwoIntroduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
 
8. java script
8. java script8. java script
8. java script
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
 
Introduction to Java Script
Introduction to Java ScriptIntroduction to Java Script
Introduction to Java Script
 
Unchallengeable miracle of Holy Quran
Unchallengeable miracle of  Holy QuranUnchallengeable miracle of  Holy Quran
Unchallengeable miracle of Holy Quran
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
 
Big Bang Theory
Big Bang TheoryBig Bang Theory
Big Bang Theory
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Chapter 1 - How the world begin
Chapter 1 - How the world beginChapter 1 - How the world begin
Chapter 1 - How the world begin
 
Big Bang Theorychandler
Big Bang TheorychandlerBig Bang Theorychandler
Big Bang Theorychandler
 
Large-Scale JavaScript Development
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript Development
 
Qur’an and its sciences
Qur’an and its sciencesQur’an and its sciences
Qur’an and its sciences
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
The Quran and Computational Linguistics
The Quran and Computational LinguisticsThe Quran and Computational Linguistics
The Quran and Computational Linguistics
 

Similar a An Introduction to JavaScript: Week 3

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
Robert Nyman
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
gerardkortney
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 

Similar a An Introduction to JavaScript: Week 3 (20)

jQuery
jQueryjQuery
jQuery
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
Node.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer NäheNode.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer Nähe
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
 
Intro to Advanced JavaScript
Intro to Advanced JavaScriptIntro to Advanced JavaScript
Intro to Advanced JavaScript
 
Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
 
JQuery
JQueryJQuery
JQuery
 
J Query
J QueryJ Query
J Query
 

Más de Event Handler (8)

Selling UCD: Getting buy-in and measuring the value of UX
Selling UCD: Getting buy-in and measuring the value of UXSelling UCD: Getting buy-in and measuring the value of UX
Selling UCD: Getting buy-in and measuring the value of UX
 
Best Practice for UX Deliverables - 2014
Best Practice for UX Deliverables - 2014Best Practice for UX Deliverables - 2014
Best Practice for UX Deliverables - 2014
 
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip HopFrom Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
 
Tumours and Tree Trunks - GeekyScience: Evolution
Tumours and Tree Trunks - GeekyScience: EvolutionTumours and Tree Trunks - GeekyScience: Evolution
Tumours and Tree Trunks - GeekyScience: Evolution
 
Best Practice for UX Deliverables
Best Practice for UX DeliverablesBest Practice for UX Deliverables
Best Practice for UX Deliverables
 
The Missing Ingredient
The Missing IngredientThe Missing Ingredient
The Missing Ingredient
 
Productivity quickly
Productivity quicklyProductivity quickly
Productivity quickly
 
Anna pickard geekyfinal
Anna pickard geekyfinalAnna pickard geekyfinal
Anna pickard geekyfinal
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

An Introduction to JavaScript: Week 3

  • 2. Recap var element = document.getElementById('puzzle'); var elements = document.getElementsByTagName('div'); var elements = document.getElementsByClassName('submit'); // not IE < 9 var element = document.querySelector('#reload .submit'); // not IE < 8 var elements = document.querySelectorAll('#reload .submit'); // not IE < 8 element.getElementById('child'); element.getElementsByTagName('span'); element.getElementsByClassName('foo'); element.querySelector('#reload .submit'); // not IE < 8 element.querySelectorAll('#reload .submit'); // not IE < 8a Friday, 11 October 13
  • 3. Recap var children = element.children; var parent = element.parentNode; Friday, 11 October 13
  • 4. Recap var parent = document.getElementById('foobar'); var element = document.createElement('div'); parent.appendChild(element); parent.insertBefore(element, someOtherElement); parent.removeChild(element); element.parentNode.removeChild(element); element.textContent = 'hello world'; Friday, 11 October 13
  • 5. Recap var ol = document.createElement('ol') , el ; document.getElementById('output').appendChild(ol); for (var i = 1; i <= 100; ++i) { el = document.createElement('li'); if (i % 3 === 0 && i % 5 === 0) { el.textContent = 'FizzBuzz'; } else if (i % 3 === 0) { el.textContent = 'Fizz'; } else if (i % 5 === 0) { el.textContent = 'Buzz'; } else { el.textContent = i; } ol.appendChild(el); } Friday, 11 October 13
  • 6. Recap var el = document.getElementById('output'); el.style.backgroundImage = "url('bg.jpg')"; el.style.fontWeight = ‘bold’; el.style.color = ‘red’; window.getComputedStyle(el).backgroundImage; // not IE < 9 el.currentStyle.backgroundImage; // IE < 9 Friday, 11 October 13
  • 7. Recap var items = ol.children ; for (var i = 0; i < items.length; ++i) { el = items[i]; if ( el.textContent === 'FizzBuzz' || el.textContent === 'Fizz' || el.textContent === 'Buzz' ) { el.style.fontWeight = 'bold'; } if (i % 2 === 0) { el.style.color = 'red'; } } Friday, 11 October 13
  • 9. Nested Loops var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]] ; for (var x = 0; x < data.length; ++x) { for (var y = 0; y < data[x].length; ++y) { console.log(data[x][y]); } } Friday, 11 October 13
  • 10. Element Property var el = document.getElementById('output'); el.id; el.className; Friday, 11 October 13
  • 11. Math is Hard Friday, 11 October 13
  • 12. Math is Hard .item { display: block; position: absolute; line-height: 40px; text-align: center; } #output { position: relative; overflow: hidden; border: 1px solid #000; margin: 20px auto; } Friday, 11 October 13
  • 13. Math is Hard var x , y , i = 0 , pieceWidth = 40 , pieceHeight = 40 , el = document.getElementById('output') , size = 11 ; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) { child = document.createElement('div'); child.style.width = pieceWidth + 'px'; child.style.height = pieceHeight + 'px'; child.style.left = (x * pieceWidth) + 'px'; child.style.top = (y * pieceHeight) + 'px'; child.className = 'item' // to be continued... Friday, 11 October 13
  • 14. Math is Hard if (x === 0 || y === 0) { child.style.backgroundColor = '#eee'; } if (x === 0 && y === 0) { child.innerHTML = '+' } else { child.textContent = x + y; } el.appendChild(child); i++; } } el.style.width = pieceWidth * size + 'px'; el.style.height = pieceHeight * size + 'px'; Friday, 11 October 13
  • 15. Element Attribute var el = document.getElementById('output'); el.setAttribute('lang', 'en'); el.getAttribute('lang'); // <div id="output" lang="en"></div> el.setAttribute('data-foo', 'foo'); el.getAttribute('data-foo'); // <div id="output" data-foo="foo"></div> Friday, 11 October 13
  • 17. Slide Puzzle var x , y , i = 0 , bgX , bgY , size = 5 , pieceWidth = Math.floor(612 / size) , pieceHeight = Math.floor(612 / size) , el = document.getElementById('output') ; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) { if (x == size - 1 && y == size - 1) { continue; } bgX = pieceWidth * x; bgY = pieceHeight * y; child = document.createElement('div'); // to be continued... Friday, 11 October 13
  • 18. Slide Puzzle child.style.backgroundPosition = "-" + bgX + 'px -' + bgY + 'px'; child.style.width = pieceWidth + 'px'; child.style.height = pieceHeight + 'px'; child.style.left = (x * pieceWidth) + 'px'; child.style.top = (y * pieceHeight) + 'px'; child.className = 'piece' child.setAttribute('data-x', x); child.setAttribute('data-y', y); el.appendChild(child); } } el.style.width = pieceWidth * size + 'px'; el.style.height = pieceHeight * size + 'px'; Friday, 11 October 13
  • 20. Events var el = document.getElementById('output'); el.addEventListener('click', callback, false); // not IE < 9 el.attachEvent('onclick', callback); // IE < 9 Friday, 11 October 13
  • 21. Events var el = document.getElementById('output'); function callback(e) { alert('hello world'); e.preventDefault(); } el.addEventListener('click', callback, false); Friday, 11 October 13
  • 23. Greeter <div id="output"> <form id="form"> <label for="input">Name</label> <input id="input" type="text"> <button type="submit">Greet</button> </form> </div> Friday, 11 October 13
  • 24. Moar Math! Friday, 11 October 13
  • 25. Moar Math! Math.round(0.5); // 1 Math.floor(0.9); // 0 Math.ceil(0.1); // 1 Math.abs(-1); // 1 Math.sqrt(9); // 3 Math.sin(1); // 0.8414709848078965 Math.cos(1); // 0.5403023058681398 Math.tan(1); // 1.5574077246549023 Math.asin(1); // 1.5707963267948966 Math.acos(1); // 0 Math.atan(1); // 0.7853981633974483 Math.min(1, 5); // 1 Math.max(1, 5); // 5 Math.PI; // 3.141592653589793 Math.E; // 2.718281828459045 Friday, 11 October 13
  • 26. Slide Puzzle .piece { background-image: url('../img/image.jpg'); display: block; position: absolute; transition: top 1s, left 1s; } #output { position: relative; overflow: hidden; border: 1px solid #000; margin: 20px auto; } Friday, 11 October 13
  • 28. Thats All Folks email: contact@danielknell.co.uk twitter: @danielknell website: http://danielknell.co.uk/ Friday, 11 October 13