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
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); } }; } ());
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
The API • addState, addStates - Add Responsive states • removeState, removeStates - Remove Responsive states • ready - tell SSM the states are added and you are ready
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
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