adaptUILayout.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * 使用说明:
  3. * 本程序以完成固定宽度布局的网页在iPhone/Android设备上浏览时可以适配设备屏幕宽度(竖屏浏览, 暂未支持横屏浏览)
  4. * 为目的。正常运行的环境是: iPhone/Android设备的自带浏览器.
  5. * 如有问题,意见或建议,请到我的博客页面留言,或发送邮件.
  6. * 其他移动版浏览器的适配问题, 不在本程序处理范围, 若有相关问题, 请留言或发送邮件.
  7. *
  8. * 博客页面地址:
  9. * http://www.cnblogs.com/plums/archive/2013/01/10/WebApp-fixed-width-layout-of-multi-terminal-adapter-since.html
  10. * 邮箱: limuchen12@126.com
  11. *
  12. * 在引入本程序后,请执行如下两步操作:
  13. * 1、对于js不能够正确获取到屏幕宽度的设备, 请使用
  14. * adaptUILayout.regulateScreen.add(设备name, 设备userAgent字符串标示或正则, {
  15. * width : 设备width,
  16. * height : 设备height
  17. * });
  18. *
  19. * //Example:
  20. * adaptUILayout.regulateScreen.add('三星 I9100G', 'GT-I9100G', {
  21. * width : 480,
  22. * height : 800
  23. * });
  24. * 录入设备屏幕尺寸, 以便程序可以正确的处理适配.
  25. * 也可以将已知设备的尺寸都录入程序.
  26. *
  27. * ### 对于如上数据, 大家也可以到我的博客页面留言板块贴出来与大家分享,
  28. * 我会收集并添加到程序中去, 以互相帮助, 减少大家的整体工作量. 谢谢!
  29. *
  30. * 2、使用如下代码启动适配
  31. * adaptUILayout.adapt(布局宽度);
  32. *
  33. * Example:
  34. * adaptUILayout.adapt(480);
  35. *
  36. */
  37. var adaptUILayout = (function(){
  38. //根据校正appVersion或userAgent校正屏幕分辨率宽度值
  39. var regulateScreen = (function(){
  40. var cache = {};
  41. //默认尺寸
  42. var defSize = {
  43. width : window.screen.width,
  44. height : window.screen.height
  45. };
  46. var ver = window.navigator.appVersion;
  47. var _ = null;
  48. var check = function(key){
  49. return key.constructor == String ? ver.indexOf(key) > -1 : ver.test(key);
  50. };
  51. var add = function(name, key, size){
  52. if(name && key)
  53. cache[name] = {
  54. key : key,
  55. size : size
  56. };
  57. };
  58. var del = function(name){
  59. if(cache[name])
  60. delete cache[name];
  61. };
  62. var cal = function(){
  63. if(_ != null)
  64. return _;
  65. for(var name in cache){
  66. if(check(cache[name].key)){
  67. _ = cache[name].size;
  68. break;
  69. }
  70. }
  71. if(_ == null)
  72. _ = defSize;
  73. return _;
  74. };
  75. return {
  76. add : add,
  77. del : del,
  78. cal : cal
  79. };
  80. })();
  81. //实现缩放
  82. var adapt = function(uiWidth){
  83. var
  84. deviceWidth,
  85. devicePixelRatio,
  86. targetDensitydpi,
  87. //meta,
  88. initialContent,
  89. head,
  90. viewport,
  91. ua;
  92. ua = navigator.userAgent.toLowerCase();
  93. //whether it is the iPhone or iPad
  94. isiOS = ua.indexOf('ipad') > -1 || ua.indexOf('iphone') > -1;
  95. //获取设备信息,并矫正参数值
  96. devicePixelRatio = window.devicePixelRatio;
  97. deviceWidth = regulateScreen.cal().width;
  98. //获取最终dpi
  99. targetDensitydpi = uiWidth / deviceWidth * devicePixelRatio * 160;
  100. //use viewport width attribute on the iPhone or iPad device
  101. //use viewport target-densitydpi attribute on the Android device
  102. initialContent = isiOS
  103. //? 'target-densitydpi=device-dpi, width=' + uiWidth + 'px, user-scalable=no'
  104. ? 'target-densitydpi=device-dpi, width=device-width, user-scalable=no'
  105. : 'target-densitydpi=' + targetDensitydpi + ', width=device-width, user-scalable=no';
  106. //: 'target-densitydpi=' + targetDensitydpi + ', width=' + uiWidth + 'px, user-scalable=no';
  107. //add a new meta node of viewport in head node
  108. head = document.getElementsByTagName('head');
  109. viewport = document.createElement('meta');
  110. viewport.name = 'viewport';
  111. viewport.content = initialContent;
  112. head.length > 0 && head[head.length - 1].appendChild(viewport);
  113. };
  114. return {
  115. regulateScreen : regulateScreen,
  116. adapt : adapt
  117. };
  118. })();