You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

149 lines
4.9 KiB

  1. /*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. MIT license */
  2. window.matchMedia || (window.matchMedia = function() {
  3. "use strict";
  4. // For browsers that support matchMedium api such as IE 9 and webkit
  5. var styleMedia = (window.styleMedia || window.media);
  6. // For those that don't support matchMedium
  7. if (!styleMedia) {
  8. var style = document.createElement('style'),
  9. script = document.getElementsByTagName('script')[0],
  10. info = null;
  11. style.type = 'text/css';
  12. style.id = 'matchmediajs-test';
  13. if (!script) {
  14. document.head.appendChild(style);
  15. } else {
  16. script.parentNode.insertBefore(style, script);
  17. }
  18. // 'style.currentStyle' is used by IE <= 8 and 'window.getComputedStyle' for all other browsers
  19. info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
  20. styleMedia = {
  21. matchMedium: function(media) {
  22. var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';
  23. // 'style.styleSheet' is used by IE <= 8 and 'style.textContent' for all other browsers
  24. if (style.styleSheet) {
  25. style.styleSheet.cssText = text;
  26. } else {
  27. style.textContent = text;
  28. }
  29. // Test if media query is true or false
  30. return info.width === '1px';
  31. }
  32. };
  33. }
  34. return function(media) {
  35. return {
  36. matches: styleMedia.matchMedium(media || 'all'),
  37. media: media || 'all'
  38. };
  39. };
  40. }());
  41. /*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */
  42. (function() {
  43. // Bail out for browsers that have addListener support
  44. if (window.matchMedia && window.matchMedia('all').addListener) {
  45. return false;
  46. }
  47. var localMatchMedia = window.matchMedia,
  48. hasMediaQueries = localMatchMedia('only all').matches,
  49. isListening = false,
  50. timeoutID = 0, // setTimeout for debouncing 'handleChange'
  51. queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used
  52. handleChange = function(evt) {
  53. // Debounce
  54. clearTimeout(timeoutID);
  55. timeoutID = setTimeout(function() {
  56. for (var i = 0, il = queries.length; i < il; i++) {
  57. var mql = queries[i].mql,
  58. listeners = queries[i].listeners || [],
  59. matches = localMatchMedia(mql.media).matches;
  60. // Update mql.matches value and call listeners
  61. // Fire listeners only if transitioning to or from matched state
  62. if (matches !== mql.matches) {
  63. mql.matches = matches;
  64. for (var j = 0, jl = listeners.length; j < jl; j++) {
  65. listeners[j].call(window, mql);
  66. }
  67. }
  68. }
  69. }, 30);
  70. };
  71. window.matchMedia = function(media) {
  72. var mql = localMatchMedia(media),
  73. listeners = [],
  74. index = 0;
  75. mql.addListener = function(listener) {
  76. // Changes would not occur to css media type so return now (Affects IE <= 8)
  77. if (!hasMediaQueries) {
  78. return;
  79. }
  80. // Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8)
  81. // There should only ever be 1 resize listener running for performance
  82. if (!isListening) {
  83. isListening = true;
  84. window.addEventListener('resize', handleChange, true);
  85. }
  86. // Push object only if it has not been pushed already
  87. if (index === 0) {
  88. index = queries.push({
  89. mql: mql,
  90. listeners: listeners
  91. });
  92. }
  93. listeners.push(listener);
  94. };
  95. mql.removeListener = function(listener) {
  96. for (var i = 0, il = listeners.length; i < il; i++) {
  97. if (listeners[i] === listener) {
  98. listeners.splice(i, 1);
  99. }
  100. }
  101. };
  102. return mql;
  103. };
  104. }());
  105. window.mediaCheck = function(options) {
  106. var mq;
  107. function mqChange(mq, options) {
  108. if (mq.matches) {
  109. if (typeof options.entry === "function") {
  110. options.entry();
  111. }
  112. } else if (typeof options.exit === "function") {
  113. options.exit();
  114. }
  115. };
  116. mq = window.matchMedia(options.media);
  117. mq.addListener(function() {
  118. mqChange(mq, options);
  119. });
  120. mqChange(mq, options);
  121. };