SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie How to matchMedia

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 -...Codemotion
 
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 WebEduardo Shiota Yasuda
 
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 desktopbetabeers
 
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 designFrédéric Harper
 
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 featuresAndreas Bovens
 
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 & FriendsRachel Andrew
 
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 FriendsFITC
 
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 - WEBU17Rachel Andrew
 
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 - NordicJSRachel Andrew
 
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.2011Patrick Lauke
 
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)Christian Rokitta
 
The Future of Responsive Design Standards
The Future of Responsive Design StandardsThe Future of Responsive Design Standards
The Future of Responsive Design StandardsBrian Fegan
 
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 EventRobert Nyman
 
New CSS Meets the Real World
New CSS Meets the Real WorldNew CSS Meets the Real World
New CSS Meets the Real WorldRachel Andrew
 
Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.GaziAhsan
 
Responsive Web Design & APEX Theme 25
Responsive Web Design & APEX Theme 25Responsive Web Design & APEX Theme 25
Responsive Web Design & APEX Theme 25Christian Rokitta
 
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 - DevFest17Rachel Andrew
 
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 & FriendsRachel Andrew
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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 DevelopmentsTrustArc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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 StreamsRoshan Dwivedi
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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 2024Rafal Los
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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 WorkerThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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 Scriptwesley chun
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Kürzlich hochgeladen (20)

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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 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
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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?