matchMedia.js

matchMedia polyfill for testing media queries in JS

MIT License

Downloads
314.1K
Stars
2.4K
Committers
26

matchMedia() polyfill

test whether a CSS media type or media query applies

Usage

// Likely want to requier both polyfills..
require('matchmedia-polyfill');
require('matchmedia-polyfill/matchMedia.addListener');

test 'tv' media type

if (matchMedia('tv').matches) {
  // tv media type supported
}

test a mobile device media query

if (matchMedia('only screen and (max-width: 480px)').matches) {
  // smartphone/iphone... maybe run some small-screen related dom scripting?
}

test landscape orientation

if (matchMedia('all and (orientation:landscape)').matches) {
  // probably tablet in widescreen view
}

Used in:

How about resizing the browser?

Paul Hayes tackled this using CSS transitions and their transitionEnd event

His code: https://github.com/fofr/matchMedia.js -- though currently it doesnt support IE6-9, since they dont have transitions, obviously. :)


Package Rankings
Top 17.02% on Repo1.maven.org
Top 1.03% on Npmjs.org
Top 6.66% on Proxy.golang.org
Top 1.96% on Bower.io
Badges
Extracted from project README
npm