SlideShare une entreprise Scribd logo
1  sur  18
matchMedia
What is it?
What you could do with it
Quirks
Browser support
matchMedia is a method for testing css media
queries in javascript
@media screen and (min-width: 500px) {
...
}
CSS snippet:
matchMedia('screen and (min-width: 500px)');
Javascript snippet:
<link media="screen and (min-width: 500px)"
href="project.css" rel="stylesheet" />
Why would I use matchMedia?
Trigger or remove functionality at breakpoints such as
navigation, dialogs, images or use your imagination.
Anatomy of a MediaQueryList object
var mql = matchMedia('screen and (min-width: 500px)');
mql has the following properties:

matches 
 
: <Boolean getter>
media
 
 
: <String>
addListener 
 
: <Function>
removeListener 
: <Function>
MediaQueryList + addListener
var mql = matchMedia('screen and (min-width: 500px)'),
handleMediaQuery = function(mql) {
if (mql.matches) {
// Media query matches
} else {
// Media query does not match anymore
}
};
mql.addListener(handleMediaQuery);
What triggers a MediaQueryList listener
width: 
 
width, min-width, max-width
height: 
 
height, min-height, max-height
device-width: 
device-width, min-device-width, max-device-width
device-height: 
device-height, min-device-height, max-device-height
aspect-ratio: 
aspect-ratio, min-aspect-ratio, max-aspect-ratio
device-aspect-ratio:
device-aspect-ratio, min-device-aspect-ratio, max-device-aspect-ratio
orientation: 
orientation
resolution: 
resolution, min-resolution, max-resolution
device-pixel-ratio: 
device-pixel-ratio, min-device-pixel-ratio, max-device-pixel-ratio
http://www.w3.org/TR/css3-mediaqueries/
Alternative to addListener
var mql = matchMedia('screen and (min-width: 500px)'),
update = function() {
// Debounced function, see slide links
if (mql.matches) {
// Media query matches
} else {
// Media query does not match anymore
}
};
window.addEventListener('resize', update, false);
http://davidwalsh.name/function-debounce
Browser quirks: OSX Chrome 27 and Firefox 22
Incorrect property name
matchMedia('(minwidth: 1800px)');
Chrome
{ matches: false, media: "not all" }
Firefox 
{ matches: false, media: "not all" }
Missing leading '('
matchMedia('width: 1800px)');
Chrome
{ matches: false, media: "invalid" }
Firefox 
{ matches: false, media: "not all" }
Missing '()'
matchMedia('width: 1800px');
Chrome
{ matches: false, media: "invalid" }
Firefox 
{ matches: false, media: "not all" }
No spaces. Spec indicates spaces are not required...
matchMedia('(min-width:0px)and(max-width:1800px)');
Chrome
{ matches: true, media: "(max-width: 1800px) and (min-width: 0px)" }
Firefox 
{ matches: false, media: "not all" }
http://www.w3.org/TR/css3-mediaqueries/#syntax
One space only
matchMedia('(min-width:0px)and (max-width:1800px)');
Chrome
{ matches: true, media: "(max-width: 1800px) and (min-width: 0px)" }
Firefox 
{ matches: true, media: "(min-width: 0px) and (max-width: 1800px)" }
Notice how the properties are sorted
matchMedia('(min-width: 0px) and (max-width: 1800px)');
Chrome
{ matches: true, media: "(max-width: 1800px) and (min-width: 0px)" }
Firefox 
{ matches: true, media: "(min-width: 0px) and (max-width: 1800px)" }
http://dev.w3.org/csswg/cssom/#serializing-media-queries
What support do we have for matchMedia?
ü  Android stock browser *
ü  Chrome 9+
ü  Chrome beta on Android *
ü  Firefox 6+
ü  Firefox mobile
ü  IE 10+
ü  Safari 5.1+ *
ü  Safari 5 on iOS *
http://caniuse.com/css-mediaqueries
http://caniuse.com/matchmedia
https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/
* Note: doesn’t support addListener
What do we do for the rest?
Maybe nothing. Depends what you expect matchMedia to do. 
Polyfills:
A polyfill is code that provides functionality that is not native to the browser.

https://github.com/weblinc/media-match
https://github.com/paulirish/matchMedia.js/
If matchMedia is required, what can I do?
http://www.w3.org/TR/css3-mediaqueries/
http://davidwalsh.name/function-debounce
http://www.w3.org/TR/css3-mediaqueries/#syntax
http://dev.w3.org/csswg/cssom/#serializing-media-queries
http://caniuse.com/css-mediaqueries
http://caniuse.com/matchmedia
https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/
https://github.com/weblinc/media-match
https://github.com/paulirish/matchMedia.js/
Presentation links

Contenu connexe

Similaire à How to matchMedia

The specs behind the sex, mobile-friendly layout beyond the desktop
The specs behind the sex, mobile-friendly layout beyond the desktopThe specs behind the sex, mobile-friendly layout beyond the desktop
The specs behind the sex, mobile-friendly layout beyond the desktop
betabeers
 
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your designSCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
Frédéric Harper
 

Similaire à How to matchMedia (20)

Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
 
Responsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da WebResponsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da Web
 
The specs behind the sex, mobile-friendly layout beyond the desktop
The specs behind the sex, mobile-friendly layout beyond the desktopThe specs behind the sex, mobile-friendly layout beyond the desktop
The specs behind the sex, mobile-friendly layout beyond the desktop
 
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your designSCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
SCREENS - 2012-09-28 - Responsive Web Design, get the best from your design
 
Intro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS featuresIntro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS features
 
404.ie: Solving Layout Problems with CSS Grid & Friends
404.ie: Solving Layout Problems with CSS Grid & Friends404.ie: Solving Layout Problems with CSS Grid & Friends
404.ie: Solving Layout Problems with CSS Grid & Friends
 
Solving Layout Problems With CSS Grid and Friends
Solving Layout Problems With CSS Grid and FriendsSolving Layout Problems With CSS Grid and Friends
Solving Layout Problems With CSS Grid and Friends
 
Solving Layout Problems with CSS Grid & Friends - WEBU17
Solving Layout Problems with CSS Grid & Friends - WEBU17Solving Layout Problems with CSS Grid & Friends - WEBU17
Solving Layout Problems with CSS Grid & Friends - WEBU17
 
Solving Layout Problems with CSS Grid & Friends - NordicJS
Solving Layout Problems with CSS Grid & Friends - NordicJSSolving Layout Problems with CSS Grid & Friends - NordicJS
Solving Layout Problems with CSS Grid & Friends - NordicJS
 
Accelerated Stylesheets
Accelerated StylesheetsAccelerated Stylesheets
Accelerated Stylesheets
 
Adaptive layouts - standards>next Manchester 23.03.2011
Adaptive layouts - standards>next Manchester 23.03.2011Adaptive layouts - standards>next Manchester 23.03.2011
Adaptive layouts - standards>next Manchester 23.03.2011
 
Responsive Web Design & APEX Theme 25 (OGh APEX World 2014)
Responsive Web Design & APEX Theme 25 (OGh APEX World 2014)Responsive Web Design & APEX Theme 25 (OGh APEX World 2014)
Responsive Web Design & APEX Theme 25 (OGh APEX World 2014)
 
The Future of Responsive Design Standards
The Future of Responsive Design StandardsThe Future of Responsive Design Standards
The Future of Responsive Design Standards
 
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile EventHTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
 
New CSS Meets the Real World
New CSS Meets the Real WorldNew CSS Meets the Real World
New CSS Meets the Real World
 
Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.
 
Rwd slidedeck
Rwd slidedeckRwd slidedeck
Rwd slidedeck
 
Responsive Web Design & APEX Theme 25
Responsive Web Design & APEX Theme 25Responsive Web Design & APEX Theme 25
Responsive Web Design & APEX Theme 25
 
Solving Layout Problems with CSS Grid & Friends - DevFest17
Solving Layout Problems with CSS Grid & Friends - DevFest17Solving Layout Problems with CSS Grid & Friends - DevFest17
Solving Layout Problems with CSS Grid & Friends - DevFest17
 
View Source London: Solving Layout Problems with CSS Grid & Friends
View Source London: Solving Layout Problems with CSS Grid & FriendsView Source London: Solving Layout Problems with CSS Grid & Friends
View Source London: Solving Layout Problems with CSS Grid & Friends
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

How to matchMedia

  • 1. matchMedia What is it? What you could do with it Quirks Browser support
  • 2. matchMedia is a method for testing css media queries in javascript
  • 3. @media screen and (min-width: 500px) { ... } CSS snippet: matchMedia('screen and (min-width: 500px)'); Javascript snippet: <link media="screen and (min-width: 500px)" href="project.css" rel="stylesheet" />
  • 4. Why would I use matchMedia? Trigger or remove functionality at breakpoints such as navigation, dialogs, images or use your imagination.
  • 5. Anatomy of a MediaQueryList object var mql = matchMedia('screen and (min-width: 500px)'); mql has the following properties: matches : <Boolean getter> media : <String> addListener : <Function> removeListener : <Function>
  • 6. MediaQueryList + addListener var mql = matchMedia('screen and (min-width: 500px)'), handleMediaQuery = function(mql) { if (mql.matches) { // Media query matches } else { // Media query does not match anymore } }; mql.addListener(handleMediaQuery);
  • 7. What triggers a MediaQueryList listener width: width, min-width, max-width height: height, min-height, max-height device-width: device-width, min-device-width, max-device-width device-height: device-height, min-device-height, max-device-height aspect-ratio: aspect-ratio, min-aspect-ratio, max-aspect-ratio device-aspect-ratio: device-aspect-ratio, min-device-aspect-ratio, max-device-aspect-ratio orientation: orientation resolution: resolution, min-resolution, max-resolution device-pixel-ratio: device-pixel-ratio, min-device-pixel-ratio, max-device-pixel-ratio http://www.w3.org/TR/css3-mediaqueries/
  • 8. Alternative to addListener var mql = matchMedia('screen and (min-width: 500px)'), update = function() { // Debounced function, see slide links if (mql.matches) { // Media query matches } else { // Media query does not match anymore } }; window.addEventListener('resize', update, false); http://davidwalsh.name/function-debounce
  • 9. Browser quirks: OSX Chrome 27 and Firefox 22
  • 10. Incorrect property name matchMedia('(minwidth: 1800px)'); Chrome { matches: false, media: "not all" } Firefox { matches: false, media: "not all" }
  • 11. Missing leading '(' matchMedia('width: 1800px)'); Chrome { matches: false, media: "invalid" } Firefox { matches: false, media: "not all" }
  • 12. Missing '()' matchMedia('width: 1800px'); Chrome { matches: false, media: "invalid" } Firefox { matches: false, media: "not all" }
  • 13. No spaces. Spec indicates spaces are not required... matchMedia('(min-width:0px)and(max-width:1800px)'); Chrome { matches: true, media: "(max-width: 1800px) and (min-width: 0px)" } Firefox { matches: false, media: "not all" } http://www.w3.org/TR/css3-mediaqueries/#syntax
  • 14. One space only matchMedia('(min-width:0px)and (max-width:1800px)'); Chrome { matches: true, media: "(max-width: 1800px) and (min-width: 0px)" } Firefox { matches: true, media: "(min-width: 0px) and (max-width: 1800px)" }
  • 15. Notice how the properties are sorted matchMedia('(min-width: 0px) and (max-width: 1800px)'); Chrome { matches: true, media: "(max-width: 1800px) and (min-width: 0px)" } Firefox { matches: true, media: "(min-width: 0px) and (max-width: 1800px)" } http://dev.w3.org/csswg/cssom/#serializing-media-queries
  • 16. What support do we have for matchMedia? ü  Android stock browser * ü  Chrome 9+ ü  Chrome beta on Android * ü  Firefox 6+ ü  Firefox mobile ü  IE 10+ ü  Safari 5.1+ * ü  Safari 5 on iOS * http://caniuse.com/css-mediaqueries http://caniuse.com/matchmedia https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/ * Note: doesn’t support addListener
  • 17. What do we do for the rest? Maybe nothing. Depends what you expect matchMedia to do. Polyfills: A polyfill is code that provides functionality that is not native to the browser. https://github.com/weblinc/media-match https://github.com/paulirish/matchMedia.js/ If matchMedia is required, what can I do?