Editing
JavaScript/Notes/ClassnameSwap
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== ClassName Swap, Language Review, RTFM, by Garrett Smith == This class shows a simple, efficient trick using event bubbling and css cascade: === Lesson: DOM Event Flow === Event flow is defined by DOM Events specification [http://www.w3.org/TR/DOM-Level-3-Events/#event-flow DOM Event Flow] [[File: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 [http://www.brainjar.com/dhtml/events/default3.asp event phase] Example of event bubbling on a table header: [http://jsbin.com/ezEfOvaR/1] === Lesson: CSS ClassName Swap === By changing an element's <code>className</code>, multiple styles can be changed at one time. Using the descendant selector, and changing the <code>className</code> of an ancestor element, multiple elements can be updated simultaneously, with a modicum of highly efficient code. Jsbin Example: [http://jsbin.com/IYaYiTAC/4/edit document.body.className = ""] For Styles, replace a loop that applies styles to descendants by adding a class token to the nearest common ancestor. ==== Example ==== Affect styles of descendant elements by adding a class token to the nearest common ancestor.[http://jsfaq.org/notes/code-guidelines/descendant-sel.html example], [http://jibbering.com/faq/notes/code-guidelines/#design explanation]. === ClassName and ClassList === <source lang="html4strict"> <div id="foodiv" class="foo bar baz">blah</div> </source> <h4>ClassName and HTML5 ClassList</h4> <source lang="html4strict"> 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. </source> 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 ==== Calculating Selector's Specificity ([http://www.w3.org/TR/CSS2/cascade.html#specificity CSS 2.1]). ==== The classname swap example leverages the fact that class selectors (e.g. <code>.foo</code>) have higher specificity than element selectors (e.g. <code>tr</code>). 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 <blockquote cite="http://www.w3.org/TR/CSS2/cascade.html#specificity"> # 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.) # count the number of ID attributes in the selector (= b) # count the number of other attributes and pseudo-classes in the selector (= c) # count the number of element names and pseudo-elements in the selector (= d) </blockquote> === JavaScript Review === ==== Functions ==== * [http://kangax.github.io/nfe/ FunctionExpression vs FunctionDeclaration], [https://groups.google.com/forum/#!msg/comp.lang.javascript/tjVn1NjGDN8/QgOuxtAymqoJ MemberExpression], [http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1 PropertyAccessor] and [http://jibbering.com/faq/notes/square-brackets/ Square Brackets]. ==== Specifications ==== * [http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm ECMAScript 1.3] and [http://www.ecma-international.org/ecma-262/5.1/ ECMAScript 5.1] Other versions of ECMAScript, including E4X and Compact, are out of scope for this class. ==== FAQ ==== [http://jibbering.com/faq/ FAQ], is currently being ported to Github! [https://github.com/comp-lang-javascript/ Get involved] === Assignment === Based off of the [http://jibbering.com/faq/notes/code-guidelines/descendant-sel.html 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: <source lang="css"> .word { background-color: #00F; } .pdf { background-color: #F00; } .pdf.word { background-color: #D0D; } .html { background-color: #e5d426; } </source> <source lang="html4strict"> <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> </source> ==== Bonus: CSS Transitions ==== For bonus points, use [https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions CSS Transition]s for the active state of the rows and buttons. See also: [http://css-tricks.com/controlling-css-animations-transitions-javascript/ Controlling CSS Animations and Transitions with JavaScript]) by Chris Coyier ==== Extra Bonus: Gradients ==== Add CSS Gradients. [http://www.colorzilla.com/gradient-editor/ Gradient editor]
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
Special pages
Page information