env.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var Browser = (function () {
  2. function Browser() {
  3. this.firefox = false;
  4. this.ie = false;
  5. this.edge = false;
  6. this.newEdge = false;
  7. this.weChat = false;
  8. }
  9. return Browser;
  10. }());
  11. var Env = (function () {
  12. function Env() {
  13. this.browser = new Browser();
  14. this.node = false;
  15. this.wxa = false;
  16. this.worker = false;
  17. this.svgSupported = false;
  18. this.touchEventsSupported = false;
  19. this.pointerEventsSupported = false;
  20. this.domSupported = false;
  21. this.transformSupported = false;
  22. this.transform3dSupported = false;
  23. this.hasGlobalWindow = typeof window !== 'undefined';
  24. }
  25. return Env;
  26. }());
  27. var env = new Env();
  28. if (typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function') {
  29. env.wxa = true;
  30. env.touchEventsSupported = true;
  31. }
  32. else if (typeof document === 'undefined' && typeof self !== 'undefined') {
  33. env.worker = true;
  34. }
  35. else if (typeof navigator === 'undefined') {
  36. env.node = true;
  37. env.svgSupported = true;
  38. }
  39. else {
  40. detect(navigator.userAgent, env);
  41. }
  42. function detect(ua, env) {
  43. var browser = env.browser;
  44. var firefox = ua.match(/Firefox\/([\d.]+)/);
  45. var ie = ua.match(/MSIE\s([\d.]+)/)
  46. || ua.match(/Trident\/.+?rv:(([\d.]+))/);
  47. var edge = ua.match(/Edge?\/([\d.]+)/);
  48. var weChat = (/micromessenger/i).test(ua);
  49. if (firefox) {
  50. browser.firefox = true;
  51. browser.version = firefox[1];
  52. }
  53. if (ie) {
  54. browser.ie = true;
  55. browser.version = ie[1];
  56. }
  57. if (edge) {
  58. browser.edge = true;
  59. browser.version = edge[1];
  60. browser.newEdge = +edge[1].split('.')[0] > 18;
  61. }
  62. if (weChat) {
  63. browser.weChat = true;
  64. }
  65. env.svgSupported = typeof SVGRect !== 'undefined';
  66. env.touchEventsSupported = 'ontouchstart' in window && !browser.ie && !browser.edge;
  67. env.pointerEventsSupported = 'onpointerdown' in window
  68. && (browser.edge || (browser.ie && +browser.version >= 11));
  69. env.domSupported = typeof document !== 'undefined';
  70. var style = document.documentElement.style;
  71. env.transform3dSupported = ((browser.ie && 'transition' in style)
  72. || browser.edge
  73. || (('WebKitCSSMatrix' in window) && ('m11' in new WebKitCSSMatrix()))
  74. || 'MozPerspective' in style)
  75. && !('OTransition' in style);
  76. env.transformSupported = env.transform3dSupported
  77. || (browser.ie && +browser.version >= 9);
  78. }
  79. export default env;