Selectors API
Supported since IE8
Estelle Weyl | @estellevw | Github | Press → to advance, 2 for comments, 4 to read/write notes.
Selectors API
Supported since IE8
var chil = $('#bar .foo');
Natively
var el = document.querySelector('#bar'); var chil = el.querySelectorAll('.foo');
or
chil = document.querySelectorAll('#bar .foo');
Access DOM elements with standard CSS selectors.
document.querySelector(selector); elem.querySelectorAll(selector);
Examples
document.querySelector('#id, li, #slides'); el.querySelectorAll('li:nth-of-type(odd)');
jQuery
$('#slides .slide').addClass('current');
Vanilla.js
var slides = d.querySelectorAll('#slides .slide'); for(var i=0; i < slides.length; i++){ slides[i].classList.add('current'); }
var slides = document.querySelectorAll('div.slide'), topics = document.querySelectorAll('h1.count'), summary = document.querySelector("#summary"); summary.innerHTML = "There are " + slides.length + " slides covering " + topics.length + " topics.";Click me
QSA = d.querySelectorAll('#slides .slide')
if you increase # of slides, QSA.length doesn't change.
var QSA = d.querySelectorAll('.slide');
var newSlide = d.createElement('div'); newSlide.classList.add('slide'); d.getElementById('slides').appendChild(newSlide); console.log('Current QSA: ' + QSA.length);Test Me
<body contenteditable>
LocalStorage
SessionStorage
WebSQL
IndexedDB
Your whole site with AppCache
Cookies
Local and Session Storage
save.addEventListener('click', function () { window.localStorage.setItem('key', textarea.value); }, false); textarea.value = window.localStorage.getItem('key');
Save text value on the client side (crash-safe)
// place content from local storage onLoad var el = document.querySelector('#myID'); el.innerHTML = window.localStorage.getItem('key');
// show/hide or create/activate notes on #4 addNotes: function(){ var d = document; //if the textarea already exists, show it. if(d.querySelector('.current textarea.mynotes')) { d.querySelector('.current textarea.mynotes').classList.toggle('temphidden'); return; } // if the text area doesn't exist, create it var ta = d.createElement('textarea'), currentSlide = d.querySelector('.current section'), path = window.location.pathname, A = path.lastIndexOf('/') + 1, B = path.lastIndexOf('.'), firstPartOfKey, key; // create a unique key based on this page, deck & server if(B && B > A){ firstPartOfKey = path.substring(A, B); } else { firstPartOfKey = path.substring(1, path.length-1) || 'home'; } key = firstPartOfKey + window.location.hash; // if something was stored, show it ta.value = window.localStorage.getItem(key) || ''; ta.className = 'mynotes'; // save to localStorage on every keyup ta.addEventListener('keyup', function(){ window.localStorage.setItem(key,ta.value); }); // currentSlide.appendChild(ta); },
Click in the code above before scrolling
Chrome: view storage: Inspector > Local Storage
Latecomers: IE9 & Android 3
.svgbg.current { background-image: url(star.svg), url(10.svg); background-size: 100px, auto; background-position: center, top; }
Supported since IE8
#presentation { counter-reset: ten_features 0; } .count h1 { counter-increment: ten_features; } .count h1:before { content: counter(ten_features) ". "; background: url(star.svg) no-repeat -40px 2px / 250px 250px; padding: 15px; }
Prefixed in FF4, Saf6, BB10, Chr19, FF15 Android
Without prefix in FF16, IE 9+, Chrome 26
No Opera, Android or mobile Chrome love.... yet
calc()
width: calc(50% - 40px); background: linear-gradient( black calc(50% - 2px), white calc(50% - 2px), white calc(50% + 2px), black calc(50% + 2px));
min()
width: min(100px, 100%); width: min(90px + 50px, 30%);
max()
width: max(100px, 100%); width: max(90px + 50px, 30%)
toggle()
em { font-style: toggle(normal, italic); }
Charge more for old IE browser support
Firefox, Chrome and Opera only
if (navigator.getUserMedia) { navigator.getUserMedia({ video: true, audio: true, toString : function() { return "video,audio"; } }, onSuccess, onError); }
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia);
play = function(stream) { var source; if (window.webkitURL) { source = webkitURL.createObjectURL(stream); } else { source = stream; // Opera and Firefox } video.autoplay = true; video.src = source; }
function takePhoto() { var context = photo.getContext('2d'); context.drawImage(video, 0, 0, width, height); }
function savePhoto() { var data = photo.toDataURL("image/png"); data = data.replace("image/png","image/octet-stream"); document.location.href = data; }
@keyframes W { 0% { left: 0; top: 0; } 25% { top: 400px; } 50% { top: 50px; } 75% { top: 400px; } 100% { left: 80%; top: 0; } }
#selector {
animation-name: nameYouMadeUp;
animation-duration: 5s;
animation-timing-function: ease-in-out;
}
possible values:
ease linear ease-in ease-out ease-in-out cubic-bezier(x1, y1, x2, y2) step-start /* same as steps(1, start) */ step-end /* same as steps(1, end) */ steps( X, start|end) /* X = # of steps + when change of value occurs */
In the debugger
In Chrome Developer Tools
Timeline tab ➫ memory
@estellevw
http://estelle.github.com