Taken from a talk at the London Web Meet-up - Building Better Experiences with Responsive Javascript
"Responsive design has thus far focused on using media queries to alter the way our site looks using CSS. The next step is to look at how we can progressively enhance our site based on the the features of the device. This is going beyond what CSS can offer and moving into the realms of responsive javascript. There are already new browser API’s are arriving to enables this, with the matchMedia API we can target specific functionality based on whether a media query matches.
Similarly we can add extra functionality and loads additional assets based upon features like geolocation and the camera. This is not building separate sites, its just changing the functionality of a single site based on the users device to optimise their experience.
The aim of this talk is to help you learn about what you can do with responsive javascript. You will learn about how you can target specific functionality to different types of devices which enable you to optimise your user experience."
27. Methodology
• Add event to window.onresize
• Use conditional statements to detect current
width of browser
• Add debounce to resize event to
prevent it firing excessively
28. The code
//debounce missing to keep example short
$(window).on('resize', function(){
if ($('body').width() < 768) {
console.log('mobile');
}
});
29. Could get messy
• Lots of code simply placed in an
on resize event could potentially
be messy
• Need to ensure there is a clear
logical separation between
different targeted viewport sizes
30. How to achieve the
logical separation
if ($('body').width() < 768) {
isMobile();
}
!
if ($('body').width() >= 768) {
isDesktop();
}
33. Methodology
• Prepare a MediaQueryList by using a
media query with
window.matchMedia
• Add listener to MediaQueryList
• When listener fires check if its a
match or unmatch
34. The code
var mql = window.matchMedia("(max-width:768px)");
mql.addListener(function(e){
if(e.matches){
console.log('enter mobile');
}
});
50. Using the config option
ssm.addState({
maxHeight: 480,
onEnter: function(){
console.log(‘enter mobile’);
}
}).ready();
51. Summary
• Create responsive states with
predefined rules of when it should
be active
• Add onEnter, onLeave and
onResize events to a state
• Use config options to add new
tests
52.
53. What is enquire.js
• Awesome Media Queries in
JavaScript
• Uses matchMedia API
• Manages registering and
unregistering
54. Setting up enquire.js
Two methods to setup enquire.js
• Download from Github
• bower install enquire
55. The API
• register - registers a media
query attaching it to a handler
• unregister - unregisters a media
query
56. Methodology
• Register a media query with a
handler
• Handler will fire when media
query matches
• Unregister unneeded handlers
60. Summary
In summary Enquire.js allows you to
• Create listeners for media queries
• Attach match and unmatch
methods to your listeners
61. 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
62. In Summary
• Looked at two API’s we can use
for responsive JavaScript
• Looked at SimpleStateManager
and enquire.js as an option to
simplify writing responsive
JavaScript
73. Demo code from this talk at
!
https://github.com/jonathan-fielding/
ResponsiveJavaScriptTalkExamples
74. • Learn how to make your sites
responsive
• Chapter on Responsive JavaScript
• http://bitly.com/NXusZn
• 35% off eBook discount code:
B3GW3B
• 4 eBooks to give away today