Presentational jQuery
                        Balancing jQuery and CSS




Presentational jQuery                              Doug Neiner
Presentational jQuery   Doug Neiner
Presentational jQuery   Doug Neiner
Presentational jQuery
                        Balancing jQuery and CSS




Presentational jQuery                              Doug Neiner
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
jQuery CSS Primer
 • jQuery adjusts the .style property on the DOM
    element to make CSS changes
    div.style.backgroundColor = "#aaa";




Presentational jQuery                              Doug Neiner
jQuery CSS Primer
   var div = document.getElementById( "#test" );

   // JS
   div.style.backgroundColor = "#aaa";

   // jQuery
   $(div).css( "backgroundColor", "#aaa" );
   $(div).css( "background-color", "#aaa" );

   // JS
   div.style.color = "#000";
   div.style.textDecoration = "underline";

   // jQuery
   $(div).css({
     color: "#000",
     textDecoration: "underline"
   });


Presentational jQuery                              Doug Neiner
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
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
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
.css() vs. Style Sheets vs.
                         <style>
                    Choosing the Best Tool for the Task




Presentational jQuery                                     Doug Neiner
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
<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
.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
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
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
Rules of the Road
                        Avoiding Common Mistakes




Presentational jQuery                              Doug Neiner
Beware of Iteration
           Don't call .css() multiple times unless needed




Presentational jQuery                                   Doug Neiner
Incorrect Approach
   var $div = $("div");

   $div.css('backgroundColor', 'red');

   if ( some_test === true ) {
     $div.css('color', 'black');
   }

   ...

   if ( some_other_test === true ) {
     $div.css('display', 'block')
         .css('position', 'relative');
   }




Presentational jQuery                    Doug Neiner
Correct Approach
   var css = {
     backgroundColor: "red"
   };

   if ( some_test === true ) {
     css.color = "black";
   }

   ...

   if ( some_other_test === true ) {
     css.display = "block";
     css.position = "relative";
   }

   $("div").css( css );




Presentational jQuery                  Doug Neiner
Incorrect Approach
   var colors = $(".color");

   $(".filter-by-color").click( function () {
      colors.toggle();
   });




Presentational jQuery                          Doug Neiner
Correct Approach
   var color_parent = $("#list");

   $(".filter-by-color").click( function () {
      color_parent.toggleClass( "show-colors" );
   });



   .color { display: none }

   #list.show-colors .color { display: block }




Presentational jQuery                              Doug Neiner
Class Methods
 • addClass(            classNames )

 • removeClass(            classNames )

 • toggleClass(           classNames, is_true )

 • hasClass(            className )

 • is(    ".className1.className2")




Presentational jQuery                             Doug Neiner
Write code like you
                            run errands
                    Don't keep revisiting the same store
                             on the same day




Presentational jQuery                                      Doug Neiner
Setting Initial State
                             To js or no-js




Presentational jQuery                           Doug Neiner
Incorrect Approach

   $( document ).ready( function () {
      $( "#dialog, #menu, #footer" ).hide();
      $( "#progress-bar" ).show();
   });




Presentational jQuery                          Doug Neiner
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
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
twitter   @dougneiner

                         email    doug@dougneiner.com

                          web     http://dougneiner.com




Presentational jQuery                                     Doug Neiner

Presentational jQuery

  • 1.
    Presentational jQuery Balancing jQuery and CSS Presentational jQuery Doug Neiner
  • 2.
  • 3.
  • 4.
    Presentational jQuery Balancing jQuery and CSS Presentational jQuery Doug Neiner
  • 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
  • 7.
    jQuery CSS Primer var div = document.getElementById( "#test" ); // JS div.style.backgroundColor = "#aaa"; // jQuery $(div).css( "backgroundColor", "#aaa" ); $(div).css( "background-color", "#aaa" ); // JS div.style.color = "#000"; div.style.textDecoration = "underline"; // jQuery $(div).css({ color: "#000", textDecoration: "underline" }); 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. StyleSheets 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 toplugin 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 theRoad Avoiding Common Mistakes Presentational jQuery Doug Neiner
  • 18.
    Beware of Iteration Don't call .css() multiple times unless needed Presentational jQuery Doug Neiner
  • 19.
    Incorrect Approach var $div = $("div"); $div.css('backgroundColor', 'red'); if ( some_test === true ) { $div.css('color', 'black'); } ... if ( some_other_test === true ) { $div.css('display', 'block') .css('position', 'relative'); } Presentational jQuery Doug Neiner
  • 20.
    Correct Approach var css = { backgroundColor: "red" }; if ( some_test === true ) { css.color = "black"; } ... if ( some_other_test === true ) { css.display = "block"; css.position = "relative"; } $("div").css( css ); Presentational jQuery Doug Neiner
  • 21.
    Incorrect Approach var colors = $(".color"); $(".filter-by-color").click( function () { colors.toggle(); }); Presentational jQuery Doug Neiner
  • 22.
    Correct Approach var color_parent = $("#list"); $(".filter-by-color").click( function () { color_parent.toggleClass( "show-colors" ); }); .color { display: none } #list.show-colors .color { display: block } Presentational jQuery Doug Neiner
  • 23.
    Class Methods •addClass( classNames ) • removeClass( classNames ) • toggleClass( classNames, is_true ) • hasClass( className ) • is( ".className1.className2") Presentational jQuery Doug Neiner
  • 24.
    Write code likeyou run errands Don't keep revisiting the same store on the same day Presentational jQuery Doug Neiner
  • 25.
    Setting Initial State To js or no-js Presentational jQuery Doug Neiner
  • 26.
    Incorrect Approach $( document ).ready( function () { $( "#dialog, #menu, #footer" ).hide(); $( "#progress-bar" ).show(); }); 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
  • 29.
    twitter @dougneiner email doug@dougneiner.com web http://dougneiner.com Presentational jQuery Doug Neiner