browser-api.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var browsers = {
  2. orientation: {
  3. landscape: 1,
  4. portrait: 2,
  5. check: function (checkOrientation, reload) {
  6. if (!browsers.devices.isMobile) {
  7. return true;
  8. }
  9. var width = document.documentElement.clientWidth;
  10. var height = document.documentElement.clientHeight;
  11. var orientation, message;
  12. if (checkOrientation == browsers.orientation.landscape) {
  13. message = "当前游戏需要横屏显示,请您旋转屏幕后再试一次";
  14. }
  15. else {
  16. message = "当前游戏需要竖屏显示,请您旋转屏幕后再试一次";
  17. }
  18. if (width >= height) {
  19. orientation = browsers.orientation.landscape;
  20. }
  21. else {
  22. orientation = browsers.orientation.portrait;
  23. }
  24. if (orientation == checkOrientation) {
  25. return true;
  26. }
  27. else {
  28. if (reload) {
  29. var result = window.confirm(message);
  30. if (result) {
  31. window.location.reload()
  32. }
  33. }
  34. else {
  35. alert(message);
  36. }
  37. return false;
  38. }
  39. }
  40. }
  41. };
  42. var supportsOrientationChange = "onorientationchange" in window,
  43. orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
  44. window.addEventListener(orientationEvent, function () {
  45. browsers.orientation.check(browsers.orientation.portrait)
  46. }, false);
  47. (function () {
  48. var hidden, visibilityChange;
  49. if (typeof document.hidden !== "undefined") {
  50. hidden = "hidden";
  51. visibilityChange = "visibilitychange";
  52. } else if (typeof document.mozHidden !== "undefined") {
  53. hidden = "mozHidden";
  54. visibilityChange = "mozvisibilitychange";
  55. } else if (typeof document.msHidden !== "undefined") {
  56. hidden = "msHidden";
  57. visibilityChange = "msvisibilitychange";
  58. } else if (typeof document.webkitHidden !== "undefined") {
  59. hidden = "webkitHidden";
  60. visibilityChange = "webkitvisibilitychange";
  61. }
  62. function handleVisibilityChange(e) {
  63. // console.log(e);
  64. // if (!ns_egret && !ns_egret.SoundContext && !ns_egret.SoundContext.context)return;
  65. // var sound = ns_egret.SoundContext.getInstance();
  66. // if (document[hidden]) {
  67. // sound.stopMusic();
  68. // }
  69. // else {
  70. // if (sound._playingMusicName) {
  71. // sound.playMusic(sound._playingMusicName);
  72. // }
  73. // }
  74. }
  75. document.addEventListener(visibilityChange, handleVisibilityChange, false);
  76. })();
  77. (function () {
  78. var ua = navigator.userAgent.toLowerCase();
  79. browsers.devices = {};
  80. browsers.devices.isMobile = (ua.indexOf('mobile') != -1 || ua.indexOf('android') != -1);
  81. })();