window.matchMedia()方法用来检查css的mediaQuery语句,但部分浏览器不支持,可以使用第三方函数库matchMedia.js。
1. 引入
// Likely want to requier both polyfills..
require('matchmedia-polyfill');
require('matchmedia-polyfill/matchMedia.addListener');
2. 调用
1)测试移动设备媒体查询
if (matchMedia('only screen and (max-width: 480px)').matches) {
// smartphone/iphone... maybe run some small-screen related dom scripting?
}
2)测试 landscape orientation
if (matchMedia('all and (orientation:landscape)').matches) {
// probably tablet in widescreen view
}
GitHub 地址:https://github.com/paulirish/matchMedia.js