|
1 | | -/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */ |
2 | | - |
3 | | -window.matchMedia = window.matchMedia || (function( doc, undefined ) { |
4 | | - |
5 | | - "use strict"; |
6 | | - |
7 | | - var bool, |
8 | | - docElem = doc.documentElement, |
9 | | - refNode = docElem.firstElementChild || docElem.firstChild, |
10 | | - // fakeBody required for <FF4 when executed in <head> |
11 | | - fakeBody = doc.createElement( "body" ), |
12 | | - div = doc.createElement( "div" ); |
13 | | - |
14 | | - div.id = "mq-test-1"; |
15 | | - div.style.cssText = "position:absolute;top:-100em"; |
16 | | - fakeBody.style.background = "none"; |
17 | | - fakeBody.appendChild(div); |
18 | | - |
19 | | - return function(q){ |
20 | | - |
21 | | - div.innerHTML = "­<style media=\"" + q + "\"> #mq-test-1 { width: 42px; }</style>"; |
22 | | - |
23 | | - docElem.insertBefore( fakeBody, refNode ); |
24 | | - bool = div.offsetWidth === 42; |
25 | | - docElem.removeChild( fakeBody ); |
26 | | - |
27 | | - return { |
28 | | - matches: bool, |
29 | | - media: q |
| 1 | +/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. Dual MIT/BSD license */ |
| 2 | + |
| 3 | +window.matchMedia || (window.matchMedia = function() { |
| 4 | + "use strict"; |
| 5 | + |
| 6 | + // For browsers that support matchMedium api such as IE 9 and webkit |
| 7 | + var styleMedia = (window.styleMedia || window.media); |
| 8 | + |
| 9 | + // For those that don't support matchMedium |
| 10 | + if (!styleMedia) { |
| 11 | + var style = document.createElement('style'), |
| 12 | + script = document.getElementsByTagName('script')[0], |
| 13 | + info = null; |
| 14 | + |
| 15 | + style.type = 'text/css'; |
| 16 | + style.id = 'matchmediajs-test'; |
| 17 | + |
| 18 | + script.parentNode.insertBefore(style, script); |
| 19 | + |
| 20 | + // 'style.currentStyle' is used by IE <= 8 and 'window.getComputedStyle' for all other browsers |
| 21 | + info = ('getComputedStyle' in window) && window.getComputedStyle(style) || style.currentStyle; |
| 22 | + |
| 23 | + styleMedia = { |
| 24 | + matchMedium: function(media) { |
| 25 | + var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }'; |
| 26 | + |
| 27 | + // 'style.styleSheet' is used by IE <= 8 and 'style.textContent' for all other browsers |
| 28 | + if (style.styleSheet) { |
| 29 | + style.styleSheet.cssText = text; |
| 30 | + } else { |
| 31 | + style.textContent = text; |
| 32 | + } |
| 33 | + |
| 34 | + // Test if media query is true or false |
| 35 | + return info.width === '1px'; |
| 36 | + } |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + return function(media) { |
| 41 | + return { |
| 42 | + matches: styleMedia.matchMedium(media || 'all'), |
| 43 | + media: media || 'all' |
| 44 | + }; |
30 | 45 | }; |
31 | | - |
32 | | - }; |
33 | | - |
34 | | -}( document )); |
35 | | - |
36 | | - |
| 46 | +}()); |
0 commit comments