polyfill.dom.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (function(){
  2. "use strict";
  3. if (typeof Element.prototype.matches !== 'function') {
  4. Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector || function matches(selector) {
  5. var element = this;
  6. var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
  7. var index = 0;
  8. while (elements[index] && elements[index] !== element) {
  9. ++index;
  10. }
  11. return Boolean(elements[index]);
  12. };
  13. }
  14. if (typeof Element.prototype.closest !== 'function') {
  15. Element.prototype.closest = function closest(selector) {
  16. var element = this;
  17. while (element && element.nodeType === 1) {
  18. if (element.matches(selector)) {
  19. return element;
  20. }
  21. element = element.parentNode;
  22. }
  23. return null;
  24. };
  25. }
  26. // requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
  27. // MIT license
  28. var win = window;
  29. if (!win.requestAnimationFrame) {
  30. win.requestAnimationFrame = win.webkitRequestAnimationFrame;
  31. win.cancelAnimationFrame = win.webkitCancelAnimationFrame || win.webkitCancelRequestAnimationFrame;
  32. if (!win.requestAnimationFrame) {
  33. win.requestAnimationFrame = function(callback, element) {
  34. var currTime = new Date().getTime();
  35. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  36. var id = win.setTimeout(function() {
  37. callback(currTime + timeToCall);
  38. }, timeToCall);
  39. lastTime = currTime + timeToCall;
  40. return id;
  41. };
  42. }
  43. if (!win.cancelAnimationFrame) {
  44. win.cancelAnimationFrame = function(id) {
  45. clearTimeout(id);
  46. };
  47. }
  48. }
  49. })();