building better experiences with responsive javascript (jonathan fielding)

76
Building Better Experiences with Responsive JavaScript Jonathan Fielding @jonthanfielding

Upload: future-insights

Post on 08-May-2015

496 views

Category:

Technology


3 download

DESCRIPTION

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."

TRANSCRIPT

Page 1: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Building Better Experiences with Responsive JavaScript

Jonathan Fielding @jonthanfielding

Page 2: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

About me• Full stack developer • Been building responsively for

over 3 years • Worked with big brands like

Beamly, Virgin, Sony and BT

Page 3: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

I asked some developers about what responsive design

meant to them

Page 4: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

targeting different devices

Page 5: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

@media screen and (max-width: 767px){

media queries

Page 6: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

responsive grids

Page 7: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

fluid layouts

Page 8: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

These all focus on one thing

Page 9: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

How the site looks

Page 10: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

There is more to building a great responsive site than simply how it

looks

Page 11: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

We also need to consider how the site functions

Page 12: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Why change functionality• Devices come in all shapes

and sizes • Input methods vary between

devices • Device functionality ranges

Page 13: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Examples

Page 14: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

accordion on mobileopen content on desktop

Page 15: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

single col on mobileequal columns on desktop

Page 16: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

simple scrollable content on mobileparallax on desktop

Page 17: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

new page on mobilelightbox on desktop

Page 18: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

stacked content on mobile

swappable panels on desktop

Page 19: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

What do these examples tell us

Page 20: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

It’s ok to have different journeys for different devices on a responsive

site

Page 21: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Desktop journey

Page 22: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Mobile journey

Page 23: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Two ways to change functionality

• Based on viewport size • Based on the features the

device supports

Page 24: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

xHow to change functionality based

on the viewport

Page 25: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Two key browser API’s• window.onresize • window.matchMedia

Page 26: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

window.resize

Page 27: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

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

Page 28: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

The code//debounce missing to keep example short

$(window).on('resize', function(){ if ($('body').width() < 768) { console.log('mobile'); } });

Page 29: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

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

Page 30: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

How to achieve the logical separation

if ($('body').width() < 768) { isMobile(); } !

if ($('body').width() >= 768) { isDesktop(); }

Page 31: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Browser Support

IEChromeFirefox Safari Android

Page 32: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

window.matchMedia

Page 33: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

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

Page 34: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

The codevar mql = window.matchMedia("(max-width:768px)"); mql.addListener(function(e){ if(e.matches){ console.log('enter mobile'); } });

Page 35: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Browser Support

IE 10+Chrome 9+Firefox 6+ Safari 5.1+ Android 3.0+

Page 36: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Real World Usage80.63%

of users have a browser that supports matchMedia

Page 37: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Libraries

Page 38: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Two popular libraries

Page 39: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

SimpleStateManager (aka SSM)

Page 40: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

What is SSM?• Responsive State Manager for JS • Uses Resize Event • Uses concept of states • Expandable with plugins

Page 41: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Setting up SSMTwo methods to setup SSM • Download from Github • bower install SimpleStateManager

Page 42: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

The API• addState - Add Responsive

states • removeState - Remove

Responsive states • ready - tell SSM the states are

added and you are ready

Page 43: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Methodology• Prepare a state in the onEnter • Clean up a state in the

onLeave • Define a onResize event per

state (optional)

Page 44: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Adding a statessm.addState({ id: “mobile”, maxWidth: 767, onEnter: function(){ console.log(‘enter mobile’); } }).ready();

Page 45: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Removing a statessm.removeState('mobile');

Page 46: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Demo

Page 47: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Extending SSMSSM Plugins allow you to: • Extend the available state

options • Wrap jQuery Plugins to add

responsive functionality

Page 48: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Plugin Methodology• Add a custom config option • Use it like any other config option

in your states

Page 49: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Add a config optionssm.addConfigOption({name:"maxHeight", test: function(){ if(typeof this.state.maxHeight === "number" && this.state.maxHeight <= window.innerHeight){ return true; } else{ return false; } }});

Page 50: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Using the config optionssm.addState({ maxHeight: 480, onEnter: function(){ console.log(‘enter mobile’); } }).ready();

Page 51: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

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

Page 52: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)
Page 53: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

What is enquire.js• Awesome Media Queries in

JavaScript • Uses matchMedia API • Manages registering and

unregistering

Page 54: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Setting up enquire.jsTwo methods to setup enquire.js • Download from Github • bower install enquire

Page 55: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

The API• register - registers a media

query attaching it to a handler • unregister - unregisters a media

query

Page 56: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Methodology• Register a media query with a

handler • Handler will fire when media

query matches • Unregister unneeded handlers

Page 57: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Registering a queryenquire.register("(max-width: 767px)", { match : function() { console.log("enter mobile"); }, });

Page 58: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Unregistering a queryenquire.unregister("(max-width: 767px)");

Page 59: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Demo

Page 60: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

SummaryIn summary Enquire.js allows you to • Create listeners for media queries • Attach match and unmatch

methods to your listeners

Page 61: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

SSM vs Enquire.jsSimpleStateManager 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

Page 62: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

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

Page 63: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

xHow to change functionality based

device features

Page 64: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

x

Page 65: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Methodology• Detect the features that a browser

supports • Progressively enhance your site

based on the features it supports

Page 66: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Using Modernizrif(Modernizr.geolocation){ console.log('Supports GeoLocation'); }

Page 67: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Use in conjunction with SSM• Create a SSM config option using

Modernizr for the test

Page 68: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Add a config optionssm.addConfigOption({name:"touch", test: function(){ if(typeof this.state.touch === "boolean" && this.state.touch === Modernizr.touch){ return true; } else{ return false; } }});

Page 69: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Using the config optionssm.addState({ touch: true, maxWidth: 767, onEnter: function(){ console.log(‘is mobile device that supports touch events’); } }).ready();

Page 70: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

So how do we build better experiences using Responsive JavaScript?

Page 71: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

By targeting functionality based on the characteristics of a device

Page 72: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Optimising the journey our user follows to best suit the device

Page 73: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Demo code from this talk at !

https://github.com/jonathan-fielding/ResponsiveJavaScriptTalkExamples

Page 74: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

• 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

Page 75: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Any Questions

Page 76: Building Better Experiences with Responsive JavaScript (Jonathan Fielding)

Further ResourcesMDN - 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

!

Demo code from this talk at !

https://github.com/jonathan-fielding/ResponsiveJavaScriptTalkExamples