Skip to content
matchMedia polyfill for testing media queries in JS
JavaScript HTML
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Added a test for addListener support Aug 17, 2013
.npmignore add .npmignore Sep 1, 2016
LICENSE.txt update license to have @scottjehl be the copyright owner Oct 18, 2012
README.md readme: add require() hint Jun 13, 2018
bower.json 0.3.1 for bower Jun 13, 2018
matchMedia.addListener.js Remove BSD license references (#81) Aug 29, 2017
matchMedia.js Remove BSD license references (#81) Aug 29, 2017
package.json v0.3.2 May 7, 2019

README.md

npm

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. :)


You can’t perform that action at this time.