Knowing Your Toolkit
/ˈhīpərˌtekst ˈmärˌkəp ˈlaNGɡwij/
In an web page (HTML document) every element (aka: tag) is an object that lives in the Document Object Model (DOM).
Welcome!
We've got awesome content:
Tag | Description |
---|---|
<header> |
The header section of the webpage |
<h1> |
Most important heading |
<p> |
Paragraph |
<ul> |
Unordered list |
<li> |
List item |
<img> |
Image, requires a source (src ) attribute |
<a> |
Link, requires a hypertext reference (href ) attribute |
<div> |
Generic block element |
<span> |
Generic inline element |
/kaˈskādiNG stīl SHēt/
In CSS, these attributes are called properties.
Header
Subtitle
Paragraph
Another paragraph
h3{ color: #f00; } p{ letter-spacing: 5px } #subtitle{ font-style: italic; } .special{ border-top: 1px solid #0f0; }
Subtitle
Paragraph
Another paragraph
Selector | Description |
---|---|
x |
HTML Tag, such as p , header , h3 , etc. |
x y |
Descendent |
.x |
Class |
#x |
ID |
[x] |
Attribute |
[x="y"] |
Attribute with value |
::before |
Pseudo Element |
:hover |
Pseudo Class |
x + y |
Immediate sibling |
Property | Description |
---|---|
text-align |
Alignment of text; cascades |
font-size |
Size of text; cascades |
display |
How an element renders, if at all (ex. block, inline, none) |
color |
Text color; cascades |
background-color |
Background color |
padding |
Space around an element |
margin |
Space against other elements |
/ˈjävəskript,ˈjavə-/
In JavaScript, these actions are called events. In our JavaScript, we can listen to these events to execute code in response.
// HTML Coin Toss:
...
// Javascript var button = document.getElementById('coin-toss-button'); var result = document.getElementById('result'); button.addEventListener('click', function(){ result.textContent = Date.now() % 2 ? 'heads' : 'tails'; });
...
jQuery is a JavaScript library that allows you to use CSS selectors in your Javascript. (It also can do a lot more, but that's a whole other presentation).
jQuery
is aliased as $
, so you can use either one, but $
is shorter and easier to follow, so it's pretty rare to see just jQuery
.
// HTML Coin Toss:
...
// JavaScript with jQuery $('#coin-toss-button').on('click', function(){ $('#result').text(Date.now() % 2 ? 'heads' : 'tails'); });
...
Function | Description |
---|---|
$( selector ) |
Get chainable jQuery object |
.on() |
Listen to events (ex. click, hover, mouseover) |
.data() |
Get/set data attributes |
.text() |
Get/set text content |
.html() |
Get/set html content |
.hide() |
Hide elements |
.show() |
Show elements |
.animate() |
Animate elements |
"I suppose it is tempting, if the only tool you have is a hammer,
to treat everything as if it were a nail."
- Abraham Maslow
// HTML Hello World!// JavaScript $('.hidden').css('opacity', 0); $('.js-hover').on('mouseover', function(){ $('.hidden').stop(true).animate({opacity: 1}, 2000); }).on('mouseout', function(){ $('.hidden').stop(true).animate({opacity: 0}, 2000); });
Ultimately, this simple effect belongs in the CSS.
// HTML Hello World!// CSS .hidden{ opacity: 0; transition-property: opacity; transition-duration: 2s; } .hoverable:hover .hidden{ opacity: 1; }
// JavaScript $('.js-tab-content').not(':first').hide(); $('.js-tab').on('click', function(){ $('.js-tab-content').hide(); var content_id = $(this).data('tab'); $('#' + content_id).show(); });
// HTML First Content
Second Content
// CSS .tab-content, [name="tabs"]{ display: none; } [name="tabs"]:checked + .tab-content{ display: block; }
First Content
Second Content
What are the Pros and Cons of each solution?
(Tip: JavaScript is better here.)
Best Friends Forever.
(Also, we're almost done, I swear!)
// CSS .tab-content{ height: 0; opacity: 0; overflow: hidden; padding: 0; transform: translate(0, 100px); transition: all 200ms; } .tab-content.active{ height: auto; opacity: 1; transform: translate(0, 0); }
// JavaScript $('.js-tab').on('click', function(e){ $('.tab-content').removeClass('active'); var content_id = $(this).data('tab'); $('#' + content_id).addClass('active'); });
(It's okay if you were about to get up and leave.)
I don't bite.
"If you can dream it, you can do it."
- Walt Disney