Learn to balance your use of jQuery's .css with CSS Style Sheets. Covers pros, cons, and tips on when to choose one method over another. Presented at the jQuery SF Bay Area Conference 2011.
5. Presentational jQuery
• jQuery CSS Primer
• .css() vs. Style Sheets vs. <style> blocks
• Rules of the Road
Balancing jQuery and CSS
• Moving Around
Presentational jQuery Doug Neiner
6. jQuery CSS Primer
• jQuery adjusts the .style property on the DOM
element to make CSS changes
div.style.backgroundColor = "#aaa";
Presentational jQuery Doug Neiner
8. jQuery CSS Primer
• jQuery adjusts the .style property on the DOM
element
div.style.backgroundColor = "#aaa";
• It is the equivalent of setting an inline style
<div style="background-color: #aaa"> … </div>
• This overrides pretty much any rule specified
elsewhere
Presentational jQuery Doug Neiner
9. Getting CSS Values
// Get the value for #test
$( "#test" ).css( "border-top-width" );
// Get the value for the first item in the result set
$( "div" ).css( "border-top-width" );
// Get all the values in the result set
var values = $( "div" ).map( function () {
return $(this).css( "border-top-width" );
}).get();
Presentational jQuery Doug Neiner
10. Dynamic Setters
// Get the value for #test
$( "div" ).css( "border-top-width", function ( i, old ) {
return ( i + 1 ) * 2; // New Value
});
Presentational jQuery Doug Neiner
11. .css() vs. Style Sheets vs.
<style>
Choosing the Best Tool for the Task
Presentational jQuery Doug Neiner
12. Style Sheet
• Pros
• Very fast
• Can be easily overridden
• Provides a customized foundation
• Clear separation of logic and style
• Cons
• You must know the element states before hand
• You must be able to select the elements
• Is not (generally) reactive
Presentational jQuery Doug Neiner
13. <style> Block
• Pros
• Very fast
• Can be overridden
• Adds to a foundation, or provides its own
• Can be reactive
• Cons
• Less separation of logic and style
• You must be able to select the elements
Presentational jQuery Doug Neiner
14. .css() Method
• Pros
• Very convenient
• Highly dynamic and reactive
• Can act on an arbitrary selection of elements
• Cons
• Not easily overridden
• No separation of logic and style
Presentational jQuery Doug Neiner
15. Style Sheet <style> jQuery
• To set initial • BulkChanges • Totransition
state to elements between
states
• To handle • Defaults that
predictable can be • Tohandle
states overridden unpredictable
states
• Bulk changes
to elements • One and two-
off changes
to elements
Presentational jQuery Doug Neiner
16. A note to plugin developers
• If you need a lot of styles, do it in a stylesheet
• No asset path issues
• Easily customized
• Nice separation of logic and style
• Ifyou have to do it only in JS, prepend it to the
<head> in a <style> block.
Presentational jQuery Doug Neiner
17. Rules of the Road
Avoiding Common Mistakes
Presentational jQuery Doug Neiner
18. Beware of Iteration
Don't call .css() multiple times unless needed
Presentational jQuery Doug Neiner
27. Correct Approach
<html class="no-js">
…
<script>
document.documentElement.className =
document.documentElement.className.replace("no-js", "js");
</script>
#dialog, #menu, #footer { display: block }
.no-js #progress-bar,
.js #dialog, .js #menu, .js #footer { display: none }
Modernizer does this for you
Presentational jQuery Doug Neiner
28. Moving Around
jQuery Animation
. I will
ex ercise
e coding
s a liv rial on
on wa e
Thi s secti b e mate g, and
ng som queuin
blishi thod,
on pu n me
w orking nimatio
's A sing.
jQuery ea
Presentational jQuery Doug Neiner