Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Responsive Javascript

Responsive Javascript

Talk about Responsive Javascript - how we can change functionality across devices using the built in API's and Libraries

Jonathan Fielding

November 12, 2013
Tweet

More Decks by Jonathan Fielding

Other Decks in Technology

Transcript

  1. Changing Functionality Why change functionality • Offer a tailored experience

    regardless of the device • Potentially decrease bounce rate
  2. Methodology for using onresize • Add event to window.onresize •

    Use conditional statements to detect current width of browser • Add debounce to resize event to prevent it firing excessively
  3. Example //debounce missing to keep example short $(window).on('resize', function(){ if

    ($('body').width() < 768) { console.log('mobile'); } });
  4. Could get messy • Lots of code simply placed in

    an on resize event could potentially be messy • Not a clear logical separation of different states
  5. Build a state manager var stateManager = (function () {

    var state = null; var resizePage = function () { if ($('body').width() < 768) { if (state !== "mobile") { displayMobile(); } resizeMobile(); } else { if (state !== "desktop") { displayDesktop(); } resizeDesktop(); } }; var displayMobile = function () { state = "mobile"; console.log("enter mobile"); }; var displayDesktop = function () { state = "desktop"; console.log("enter desktop"); }; var resizeMobile = function () { console.log("resizing mobile"); }; var resizeDesktop = function () { console.log("resizing desktop"); }; return { init: function () { resizePage(); $(window).on('resize', resizePage); } }; } ());
  6. Methodology for using matchMedia • Prepare a MediaQueryList by using

    a media query with window.matchMedia • Add listener to MatchQueryList • When listener fires check if its a match or unmatch
  7. browser support IE - 10+ Chrome - 9+ Firefox -

    6+ Safari - 5.1+ Android - 3.0+
  8. What is SSM? • Responsive State Manager for JS •

    Uses Resize Event • Expandable with plugins
  9. Setting up SSM Two methods to setup SSM • Download

    from Github • bower install SimpleStateManager
  10. The API • addState, addStates - Add Responsive states •

    removeState, removeStates - Remove Responsive states • ready - tell SSM the states are added and you are ready
  11. Methodology for using SSM • Prepare a state in the

    onEnter • Clean up a state in the onLeave • Define a onResize event per state (optional)
  12. Extending SSM SSM Plugins allow you • Extend the available

    state options • Wrap jQuery Plugins to add responsive functionality
  13. SSM Summary In summary SSM allows you to • Create

    responsive states with predefined rules of when it should be active • Add onEnter, onLeave and onResize events to a state
  14. What is EnquireJS? • Awesome Media Queries in JavaScript •

    Uses matchMedia API • Manages registering and unregistering
  15. The API • register - registers a media query attaching

    it to a handler • unregister - unregisters a media query
  16. Methodology for using Enquire.js • Register a media query with

    a handler • Handler will fire when media query matches • Unregister unneeded handlers
  17. Enquire.js Summary In summary Enquire.js allows you to • Create

    listeners for media queries • Attach match and unmatch methods to your listeners
  18. SSM vs Enquire.js SimpleStateManager Enquire.js method onresize matchMedia browser support

    IE7+, FF, O, C, S IE10+, FF, O, C, S API events Enter, Leave, Resize Enter, Leave Plugin Library yes no
  19. Further Resources MDN - Testing Media Queries - mzl.la/18r5yGi MDN

    – MediaQueryList - mzl.la/1bxbbZJ MDN – Window.matchMedia - mzl.la/19qc3Yk SimpleStateManager – simplestatemanager.com Enquire.js - wicky.nillia.ms/enquire.js/ MatchMedia Polyfill - github.com/paulirish/matchMedia.js/ Jonathan Fielding’s Blog - jonathanfielding.com !