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.

118 lines
4.9 KiB

7 years ago
  1. /**
  2. * MobileWeb 通用功能助手包含常用的 UA 判断页面适配search 参数转 键值对
  3. * JS 应在 head 中尽可能早的引入减少重绘
  4. *
  5. * fixScreen 方法根据两种情况适配该方法自动执行
  6. * 1. 定宽 对应 meta 标签写法 -- <meta name="viewport" content="target-densitydpi=device-dpi,width=750">
  7. * 该方法会提取 width 主动添加 scale 相关属性值
  8. * 注意 如果 meta 标签中指定了 initial-scale 该方法将不做处理即不执行
  9. * 2. REM: 不用写 meta 标签该方法根据 dpr 自动生成并在 html 标签中加上 data-dpr font-size 两个属性值
  10. * 该方法约束IOS 系统最大 dpr = 3其它系统 dpr = 1页面每 dpr 最大宽度即页面宽度/dpr = 750REM 换算比值为 16
  11. * 对应 css 开发任何弹性尺寸均使用 rem 单位rem 默认宽度为 视觉稿宽度 / 16;
  12. * scss $ppr(pixel per rem) 变量写法 -- $ppr: 750px/16/1rem;
  13. * 元素尺寸写法 -- html { font-size: $ppr*1rem; } body { width: 750px/$ppr; }
  14. */
  15. window.mobileUtil = (function(win, doc) {
  16. var UA = navigator.userAgent,
  17. isAndroid = /android|adr/gi.test(UA),
  18. isIos = /iphone|ipod|ipad/gi.test(UA) && !isAndroid, // 据说某些国产机的UA会同时包含 android iphone 字符
  19. isMobile = isAndroid || isIos; // 粗略的判断
  20. return {
  21. isAndroid: isAndroid,
  22. isIos: isIos,
  23. isMobile: isMobile,
  24. isNewsApp: /NewsApp\/[\d\.]+/gi.test(UA),
  25. isWeixin: /MicroMessenger/gi.test(UA),
  26. isQQ: /QQ\/\d/gi.test(UA),
  27. isYixin: /YiXin/gi.test(UA),
  28. isWeibo: /Weibo/gi.test(UA),
  29. isTXWeibo: /T(?:X|encent)MicroBlog/gi.test(UA),
  30. tapEvent: isMobile ? 'tap' : 'click',
  31. /**
  32. * 缩放页面
  33. */
  34. fixScreen: function() {
  35. var metaEl = doc.querySelector('meta[name="viewport"]'),
  36. metaCtt = metaEl ? metaEl.content : '',
  37. matchScale = metaCtt.match(/initial\-scale=([\d\.]+)/),
  38. matchWidth = metaCtt.match(/width=([^,\s]+)/);
  39. if ( !metaEl ) { // REM
  40. var docEl = doc.documentElement,
  41. maxwidth = docEl.dataset.mw || 750, // 每 dpr 最大页面宽度
  42. dpr = isIos ? Math.min(win.devicePixelRatio, 3) : 1,
  43. scale = 1 / dpr,
  44. tid;
  45. docEl.removeAttribute('data-mw');
  46. docEl.dataset.dpr = dpr;
  47. metaEl = doc.createElement('meta');
  48. metaEl.name = 'viewport';
  49. metaEl.content = fillScale(scale);
  50. docEl.firstElementChild.appendChild(metaEl);
  51. var refreshRem = function() {
  52. var width = docEl.getBoundingClientRect().width;
  53. if (width / dpr > maxwidth) {
  54. width = maxwidth * dpr;
  55. }
  56. var rem = width / 16;
  57. docEl.style.fontSize = rem + 'px';
  58. };
  59. win.addEventListener('resize', function() {
  60. clearTimeout(tid);
  61. tid = setTimeout(refreshRem, 300);
  62. }, false);
  63. win.addEventListener('pageshow', function(e) {
  64. if (e.persisted) {
  65. clearTimeout(tid);
  66. tid = setTimeout(refreshRem, 300);
  67. }
  68. }, false);
  69. refreshRem();
  70. } else if ( isMobile && !matchScale && ( matchWidth && matchWidth[1] != 'device-width' ) ) { // 定宽
  71. var width = parseInt(matchWidth[1]),
  72. iw = win.innerWidth || width,
  73. ow = win.outerWidth || iw,
  74. sw = win.screen.width || iw,
  75. saw = win.screen.availWidth || iw,
  76. ih = win.innerHeight || width,
  77. oh = win.outerHeight || ih,
  78. ish = win.screen.height || ih,
  79. sah = win.screen.availHeight || ih,
  80. w = Math.min(iw,ow,sw,saw,ih,oh,ish,sah),
  81. scale = w / width;
  82. if ( scale < 1 ) {
  83. metaEl.content = metaCtt + ',' + fillScale(scale);
  84. }
  85. }
  86. function fillScale(scale) {
  87. return 'initial-scale=' + scale + ',maximum-scale=' + scale + ',minimum-scale=' + scale;
  88. }
  89. },
  90. /**
  91. * 转href参数成键值对
  92. * @param href {string} 指定的href默认为当前页href
  93. * @returns {object} 键值对
  94. */
  95. getSearch: function(href) {
  96. href = href || win.location.search;
  97. var data = {},reg = new RegExp( "([^?=&]+)(=([^&]*))?", "g" );
  98. href && href.replace(reg,function( $0, $1, $2, $3 ){
  99. data[ $1 ] = $3;
  100. });
  101. return data;
  102. }
  103. };
  104. })(window, document);
  105. // 默认直接适配页面
  106. mobileUtil.fixScreen();