JavaScript/Notes/ClassnameSwap

From Noisebridge
< JavaScript
Revision as of 14:00, 2 March 2014 by 199.241.200.45 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

[edit] ClassName Swap, Language Review, RTFM, by Garrett Smith

This class shows a simple, efficient trick using event bubbling and css cascade:

[edit] Lesson: DOM Event Flow

Event flow is defined by DOM Events specification DOM Event Flow

Eventflow .png

Event objects are dispatched to an event target. At the beginning of the dispatch, implementations MUST first determine the event object's propagation path.

Event flow example from Brainjar.com event phase

Example of event bubbling on a table header: [1]

[edit] Lesson: CSS ClassName Swap

By changing an element's className, multiple styles can be changed at one time.

Using the descendant selector, and changing the className of an ancestor element, multiple elements can be updated simultaneously, with a modicum of highly efficient code.

Jsbin Example: document.body.className = ""

For Styles, replace a loop that applies styles to descendants by adding a class token to the nearest common ancestor.

[edit] Example

Affect styles of descendant elements by adding a class token to the nearest common ancestor.example, explanation.


[edit] ClassName and ClassList

<div id="foodiv" class="foo bar baz">blah</div>

ClassName and HTML5 ClassList

foodiv.className; // "foo bar baz";
foodiv.classList.contains("foo"); // true;
foodiv.classList.add("bing"); // void.
foodiv.classList.remove("bar"); // void.
foodiv.classList.toggle("bing"); // void.

A fallback for older browsers that don't support HTMLElement DOMTokenList: https://github.com/GarrettS/ClassTokenList http://www.whatwg.org/specs/web-apps/2009-10-27/multipage/urls.html#domtokenlist-0

[edit] Calculating Selector's Specificity (CSS 2.1).

The classname swap example leverages the fact that class selectors (e.g. .foo) have higher specificity than element selectors (e.g. tr).

CSS Selector specificity is determined four numbers, a-b-c-d, in a number system with a large base.

a=1, b=0, c=0, and d=0

  1. count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element's "style" attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
  2. count the number of ID attributes in the selector (= b)
  3. count the number of other attributes and pseudo-classes in the selector (= c)
  4. count the number of element names and pseudo-elements in the selector (= d)

[edit] JavaScript Review

[edit] Functions

[edit] Specifications

Other versions of ECMAScript, including E4X and Compact, are out of scope for this class.

[edit] FAQ

FAQ, is currently being ported to Github! Get involved

[edit] Assignment

Based off of the example, create a table with headers that when clicked highlight the rows of that type.

For example, for a table with three headers: "word", "pdf", "html"; create a table that has rows of class "word", "pdf", and "html". The rows need not be distinguishable in their default state (they can all look the same). When the header is activated, highlight the row by changing its className.

The row types need not be mutually exclusive. For example, you could, for a row with class="word pdf" use purple, as:

.word { 
    background-color: #00F;
}
 
.pdf {
    background-color: #F00;
}
 
.pdf.word { 
    background-color: #D0D;
}
 
.html {
    background-color: #e5d426;
}
<table>
  <thead>
    <th class="word-head">Word</th>
    <th class="pdf-head">PDF</th>
    <th class="html-head">HTML</th>
  </thead>
  <tbody>
    <tr class="word-row">
      <td colspan="3">Word</td>
    </tr>
    <!-- more rows -->
</table>

[edit] Bonus: CSS Transitions

For bonus points, use CSS Transitions for the active state of the rows and buttons. See also: Controlling CSS Animations and Transitions with JavaScript) by Chris Coyier

[edit] Extra Bonus: Gradients

Add CSS Gradients. Gradient editor

Personal tools