CrashCourse:
Part 2
Brought to you by Will Crichton
jQuery
jQuery is the most popular JS library today.
It makes dealing with interactivity easy!
document.addEventListener('click', function(){
var divs = document.getElementsByTagNme('div');
for (var i = 0; i < divs.length; i++) {
var div = divs[i];
if (div.className.indexOf('box') > -1) {
div.style.backgroundColor = 'red';
}
}
});
vs
$(document).click(function() {
$('div.box').css('background-color', 'red');
}) jQuery: Basics
The $ does a lot of stuff.
$('.navbar a:active'); // I was a CSS selector
$(document); // I was a JS object
$(function() { ... }); // I was a function
// But now we're all jQuery objects!
jQuery objects are groups of elements which
we can perform jQuery operations on.
we can perform jQuery operations on.
$('.navbar a:active').css('background', 'green')
.html('I am an active link');
jQuery objects can be one or more elements.
jQuery: Manipulation
jQuery provides a number of ways to manipulate elements.
var $div = $('div');
// classes
$div.addClass('hello');
if ($div.hasClass('hello')) {
$div.removeClass('world');
}
// css
$div.css('width', '100px');
$div.css({color: 'red', marginTop: '10px'});
// changing content
$div.html('This is some content.');
$div.append('This is some additional content.');
// prepend, after, ...
jQuery: Modifying DOM
You can add/delete elements from the page.
for (var i = 0; i < 10; i++) {
var $newBox = $('<div class="hello"></div>');
$('body').append($newBox);
}
See: madness
jQuery: Events
Events are key to any sort of UI programming.
Common events include:
- click/dblclick
- focus/blur
- keydown/keyup/keypress
- mousedown/mouseup
- mouseover
- change
$('input').keypress(function(event) {
console.log(event.which, 'key was pressed');
}) jQuery: Ajax
$.ajax({
url: 'https://api.forecast.io/forecast/1b7f8a378848cf9eeaff18f73eaed6c5/40.4383,-79.997'
dataType: 'jsonp',
success: function(data) {
var conditions = data.currently;
var message = "In Pittsburgh, it's currently " +
conditions.summary + " and " +
conditions.temperature + " degrees."
alert(message);
}
});
Canvas
Drawing 2D things!
Drawing on the Web
Sometimes we need to draw complex objects
dynamically (i.e. without images).
- SVG (Scalable Vector Graphics)
- Canvas
HTML takes care of rendering for you, but here
we tell the canvas where and when to draw stuff.
we tell the canvas where and when to draw stuff.
Paper.js
Canvas is hard, so we use libraries (like jQuery).
You can do some cool stuff:
Talk to me if you're curious about the code...
Bootstrap
Bootstrap is a CSS/JS framework.
Features include:
- Grid system
- Basic styling (fonts, buttons, etc.)
- Icons
- Page elements (alerts, navbars)
- Animated components (popups)
Flask
Flask is a web server: it serves files on a port.
from Flask import *
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, world!'
if __name__ == "__main__":
app.run(debug=True)
Flask
Things you can do with Flask include:
- Handle incoming data (POST/GET)
- Query services and return data (MySQL, APIs)
- Templating engines (data -> HTML)
- Session handling (remember temp data)
- Cookies (remember data longer)
Firebase
Need persistent data? Use firebase!
var db = new Firebase('https://crashcourse.firebaseio.com/' + ROOM_ID);
db.on('child_added', function(child) {
console.log(child.val());
});
db.push({message: 'Hello, world!'}); CrashCourse: Part 2
By Will Crichton
CrashCourse: Part 2
- 859