refreshRem.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. function refreshRem() {
  2. document.documentElement.style.fontSize =
  3. Math.min(window.innerWidth, screen.width, document.body.offsetWidth) /
  4. 750 *
  5. 100 +
  6. "px";
  7. }
  8. window.addEventListener(
  9. "resize",
  10. function() {
  11. clearTimeout(tid);
  12. tid = setTimeout(refreshRem, 300);
  13. },
  14. false
  15. );
  16. window.addEventListener(
  17. "pageshow",
  18. function(e) {
  19. if (e.persisted) {
  20. clearTimeout(tid);
  21. tid = setTimeout(refreshRem, 300);
  22. }
  23. },
  24. false
  25. );
  26. var tid;
  27. document.addEventListener("DOMContentLoaded", refreshRem);
  28. if (
  29. typeof WeixinJSBridge == "object" &&
  30. typeof WeixinJSBridge.invoke == "function"
  31. ) {
  32. handleFontSize();
  33. } else {
  34. if (document.addEventListener) {
  35. document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
  36. } else if (document.attachEvent) {
  37. document.attachEvent("WeixinJSBridgeReady", handleFontSize);
  38. document.attachEvent("onWeixinJSBridgeReady", handleFontSize);
  39. }
  40. }
  41. function handleFontSize() {
  42. // 设置网页字体为默认大小
  43. WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 });
  44. // 重写设置网页字体大小的事件
  45. WeixinJSBridge.on("menu:setfont", function() {
  46. WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 });
  47. });
  48. }
  49. // window.onload = function () {
  50. // // 禁用 iOS10 双指缩放:
  51. // document.addEventListener('touchstart', function (event) {
  52. // if (event.touches.length > 1) {
  53. // event.preventDefault();
  54. // }
  55. // })
  56. // // 禁用 iOS10 手指双击缩放:
  57. // var lastTouchEnd = 0;
  58. // document.addEventListener('touchend', function (event) {
  59. // var now = (new Date()).getTime();
  60. // if (now - lastTouchEnd <= 300) {
  61. // event.preventDefault();
  62. // }
  63. // lastTouchEnd = now;
  64. // }, false)
  65. // }
  66. function addEvent(dom, event, fn, remove) {
  67. if (typeof dom === "string") {
  68. dom = document.querySelectorAll(dom);
  69. }
  70. if (dom.length) {
  71. for (var index = 0; index < dom.length; index++) {
  72. (function() {
  73. var i = index;
  74. var callback = function(e) {
  75. fn(e, i);
  76. };
  77. dom[index].addEventListener(event, callback);
  78. if (remove) dom.removeEventListener(event, callback);
  79. })();
  80. }
  81. } else {
  82. dom.addEventListener(event, fn);
  83. if (remove) dom.removeEventListener(event, fn);
  84. }
  85. }
  86. function addClass(dom, cName) {
  87. if (dom.length) {
  88. for (var index = 0; index < dom.length; index++) {
  89. _addClass(dom[index], cName);
  90. }
  91. } else {
  92. _addClass(dom, cName);
  93. }
  94. }
  95. function removeClass(dom, cName) {
  96. if (dom.length) {
  97. for (var index = 0; index < dom.length; index++) {
  98. _removeClass(dom[index], cName);
  99. }
  100. } else {
  101. _removeClass(dom, cName);
  102. }
  103. }
  104. function _hasClass(elements, cName) {
  105. return !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
  106. }
  107. function _addClass(elements, cName) {
  108. if (!_hasClass(elements, cName)) {
  109. elements.className += " " + cName;
  110. }
  111. }
  112. function _removeClass(elements, cName) {
  113. if (_hasClass(elements, cName)) {
  114. elements.className = elements.className.replace(
  115. new RegExp("(\\s+|^)" + cName + "(\\s+|$)"),
  116. " "
  117. );
  118. }
  119. }
  120. function setCookie(name, value) {
  121. var Days = 1 / 24;
  122. var exp = new Date();
  123. if (Object.prototype.toString.call(value) === "[object Object]") {
  124. value = JSON.stringify(value);
  125. }
  126. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  127. document.cookie =
  128. name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  129. }
  130. function getCookie(name) {
  131. var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"),
  132. arr = document.cookie.match(reg);
  133. if (arr) {
  134. try {
  135. return JSON.parse(unescape(arr[2]));
  136. } catch (error) {
  137. return unescape(arr[2]);
  138. }
  139. } else return "";
  140. }
  141. function getJSON(url, callback, fail) {
  142. var xhr = new XMLHttpRequest();
  143. xhr.open("GET", url);
  144. xhr.onreadystatechange = handler;
  145. xhr.responseType = "json";
  146. xhr.setRequestHeader("Accept", "application/json");
  147. xhr.send();
  148. function handler() {
  149. if (this.readyState === this.DONE) {
  150. if (this.status === 200) {
  151. callback && callback(this.response);
  152. } else {
  153. if (fail) {
  154. throw new Error(
  155. "getJSON: `" + url + "` failed with status: [" + this.status + "]"
  156. );
  157. } else {
  158. fail(
  159. new Error(
  160. "getJSON: `" + url + "` failed with status: [" + this.status + "]"
  161. )
  162. );
  163. }
  164. }
  165. }
  166. }
  167. return xhr;
  168. }
  169. /**
  170. * 简易动画 使用css3 d-none => display:none
  171. * @param {string} el 元素
  172. * @param {string} showClass 显示动画class
  173. * @param {string} hideClass 隐藏动画class
  174. */
  175. function aniFn(el, showClass, hideClass) {
  176. //hideClass = "hide"
  177. showClass = showClass || "show";
  178. hideClass = showClass || "hide";
  179. this.el = el;
  180. this.showClass = showClass;
  181. this.hideClass = hideClass;
  182. }
  183. aniFn.prototype.show = function() {
  184. removeClass(this.el, "" + this.hideClass + " d-none");
  185. addClass(this.el, this.showClass);
  186. };
  187. aniFn.prototype.hide = function() {
  188. var _this = this.el;
  189. removeClass(_this, this.showClass);
  190. addClass(_this, this.hideClass);
  191. setTimeout(addClass, 300, _this, 'd-none');
  192. }
  193. /**
  194. * 获取url参数
  195. * @param {String} name 参数名称
  196. */
  197. function getQueryString (name) {
  198. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  199. var r = window.location.search.substr(1).match(reg)
  200. if (r != null) return unescape(r[2])
  201. return null
  202. }