|
d92c1e8
paulirish authored
|
||
| 1 | /* | |
|
da923cc
fofr authored
|
||
| 2 | * MediaQueryListener proof of concept using CSS transitions by Paul Hayes | |
|
ea57cb7
fofr authored
|
||
| 3 | * November 5th 2011 | |
| 4 | * | |
| 5 | * Based on the excellent matchMedia polyfill | |
| 6 | * https://github.com/paulirish/matchMedia.js | |
| 7 | * | |
|
91bacc9
Eivind Uggedal authored
|
||
| 8 | * matchMedia() polyfill - test whether a CSS media type or media query applies | |
|
50c64c8
paulirish authored
|
||
| 9 | * authors: Scott Jehl, Paul Irish, Nicholas Zakas | |
|
77a0042
paulirish authored
|
||
| 10 | * Copyright (c) 2011 Scott, Paul and Nicholas. | |
| 11 | * Dual MIT/BSD license | |
|
91bacc9
Eivind Uggedal authored
|
||
| 12 | */ | |
|
d92c1e8
paulirish authored
|
||
| 13 | ||
|
ea57cb7
fofr authored
|
||
| 14 | mql = (function(doc, undefined) { | |
|
50c64c8
paulirish authored
|
||
| 15 | ||
|
7772d7b
Divya Manian authored
|
||
| 16 | var docElem = doc.documentElement, | |
|
ea57cb7
fofr authored
|
||
| 17 | refNode = docElem.firstElementChild || docElem.firstChild, | |
| 18 | idCounter = 0; | |
|
7772d7b
Divya Manian authored
|
||
| 19 | if(!doc.getElementById('mq-style')) { | |
| 20 | style = doc.createElement('style'); | |
| 21 | style.id = 'mq-style'; | |
| 22 | style.textContent = '.mq { -webkit-transition: width 0.001ms; -moz-transition: width 0.001ms; -o-transition: width 0.001ms; -ms-transition: width 0.001ms; width: 0; position: absolute; top: -100em; }\n'; | |
| 23 | docElem.insertBefore(style, refNode); | |
| 24 | } | |
|
154509a
paulirish authored
|
||
| 25 | ||
|
a7b7d94
John Reading authored
|
||
| 26 | var transitionEnds = Array('transitionend','webkitTransitionEnd','oTransitionEnd','msTransitionEnd'); | |
| 27 | ||
| 28 | for(var i in transitionEnds) { | |
| 29 | if ('on'+ transitionEnds[i].toLowerCase() in window) transitionEnd = transitionEnds[i]; | |
| 30 | } | |
| 31 | ||
|
ea57cb7
fofr authored
|
||
| 32 | return function(q, cb) { | |
|
154509a
paulirish authored
|
||
| 33 | ||
|
ea57cb7
fofr authored
|
||
| 34 | var id = 'mql-' + idCounter++, | |
| 35 | callback = function() { | |
| 36 | // perform test and send results to callback | |
| 37 | cb({ | |
| 38 | matches: (div.offsetWidth == 42), | |
| 39 | media: q | |
| 40 | }); | |
| 41 | }, | |
| 42 | div = doc.createElement('div'); | |
| 43 | ||
| 44 | div.className = 'mq'; // mq class in CSS declares width: 0 and transition on width of duration 0.001ms | |
| 45 | div.id = id; | |
|
7772d7b
Divya Manian authored
|
||
| 46 | style.textContent += '@media ' + q + ' { #' + div.id + ' { width: 42px; } }\n'; | |
|
ea57cb7
fofr authored
|
||
| 47 | ||
|
a7b7d94
John Reading authored
|
||
| 48 | // add transition event listener | |
| 49 | div.addEventListener(transitionEnd, callback, false); | |
|
ea57cb7
fofr authored
|
||
| 50 | ||
| 51 | docElem.insertBefore(div, refNode); | |
| 52 | ||
| 53 | // original polyfill removes element, we need to keep element for transitions to continue and events to fire. | |
| 54 | ||
| 55 | return { | |
| 56 | matches: div.offsetWidth == 42, | |
| 57 | media: q | |
| 58 | }; | |
| 59 | }; | |
| 60 | ||
| 61 | })(document); |