layout.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var adaptUILayout = (function() {
  2. //根据校正appVersion或userAgent校正屏幕分辨率宽度值
  3. var regulateScreen = (function() {
  4. var cache = {};
  5. //默认尺寸
  6. var defSize = {
  7. width: window.screen.width,
  8. height: window.screen.height
  9. };
  10. var ver = window.navigator.appVersion;
  11. var _ = null;
  12. var check = function(key) {
  13. return key.constructor == String ? ver.indexOf(key) > -1 : ver.test(key);
  14. };
  15. var add = function(name, key, size) {
  16. if (name && key)
  17. cache[name] = {
  18. key: key,
  19. size: size
  20. };
  21. };
  22. var del = function(name) {
  23. if (cache[name])
  24. delete cache[name];
  25. };
  26. var cal = function() {
  27. if (_ != null)
  28. return _;
  29. for (var name in cache) {
  30. if (check(cache[name].key)) {
  31. _ = cache[name].size;
  32. break;
  33. }
  34. }
  35. if (_ == null)
  36. _ = defSize;
  37. return _;
  38. };
  39. return {
  40. add: add,
  41. del: del,
  42. cal: cal
  43. };
  44. })();
  45. //实现缩放
  46. var adapt = function(uiWidth) {
  47. var
  48. deviceWidth,
  49. devicePixelRatio,
  50. targetDensitydpi,
  51. //meta,
  52. initialContent,
  53. head,
  54. viewport,
  55. ua;
  56. ua = navigator.userAgent.toLowerCase();
  57. //whether it is the iPhone or iPad
  58. isiOS = ua.indexOf('ipad') > -1 || ua.indexOf('iphone') > -1;
  59. //获取设备信息,并矫正参数值
  60. devicePixelRatio = window.devicePixelRatio;
  61. deviceWidth = regulateScreen.cal().width;
  62. //获取最终dpi
  63. targetDensitydpi = uiWidth / deviceWidth * devicePixelRatio * 160;
  64. //use viewport width attribute on the iPhone or iPad device
  65. //use viewport target-densitydpi attribute on the Android device
  66. initialContent = isiOS ?
  67. 'width=' + uiWidth + ', user-scalable=no' :
  68. 'target-densitydpi=' + targetDensitydpi + ', width=' + uiWidth + ', user-scalable=no';
  69. //add a new meta node of viewport in head node
  70. head = document.getElementsByTagName('head');
  71. viewport = document.createElement('meta');
  72. viewport.name = 'viewport';
  73. viewport.content = initialContent;
  74. head.length > 0 && head[head.length - 1].appendChild(viewport);
  75. };
  76. return {
  77. regulateScreen: regulateScreen,
  78. adapt: adapt
  79. };
  80. })();
  81. /*
  82. *375为当期页面指定的统一分辨率,其他分辨率下均为此分辨率的放缩变化
  83. */
  84. adaptUILayout.adapt(375);
  85. //rem初始化代码
  86. /*function adapt(designWidth, rem2px){
  87. var d = window.document.createElement('div');
  88. d.style.width = '1rem';
  89. d.style.display = "none";
  90. var head = window.document.getElementsByTagName('head')[0];
  91. head.appendChild(d);
  92. var defaultFontSize = parseFloat(window.getComputedStyle(d, null).getPropertyValue('width'));
  93. d.remove();
  94. document.documentElement.style.fontSize = window.innerWidth / designWidth * rem2px / defaultFontSize * 100 + '%';
  95. var st = document.createElement('style');
  96. var portrait = "@media screen and (min-width: "+window.innerWidth+"px) {html{font-size:"+ ((window.innerWidth/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}";
  97. var landscape = "@media screen and (min-width: "+window.innerHeight+"px) {html{font-size:"+ ((window.innerHeight/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}"
  98. st.innerHTML = portrait + landscape;
  99. head.appendChild(st);
  100. return defaultFontSize
  101. };
  102. var defaultFontSize = adapt(640, 100);
  103. */
  104. //H5翻转屏幕事件
  105. /*function orientationChange() {
  106. switch(window.orientation) {
  107.   case 0:
  108. alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);
  109. break;
  110.   case -90:
  111. alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);
  112. break;
  113.   case 90:
  114. alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);
  115. break;
  116.   case 180:
  117.   alert("风景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);
  118.   break;
  119. };
  120. };
  121. // 添加事件监听
  122. addEventListener('load', function(){
  123. orientationChange();
  124. window.onorientationchange = orientationChange;
  125. });
  126. */