yduia.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * YDUI 可伸缩布局方案
  3. * rem计算方式:设计图尺寸px / 100 = 实际rem 例: 100px = 1rem
  4. */
  5. !(function (window) {
  6. /* 设计图文档宽度 */
  7. var docWidth = 750;
  8. var doc = window.document,
  9. docEl = doc.documentElement,
  10. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
  11. var recalc = (function refreshRem() {
  12. var clientWidth = docEl.getBoundingClientRect().width;
  13. /* 8.55:小于320px不再缩小,11.2:大于420px不再放大 */
  14. docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / docWidth), 11.2), 8.55) * 5 + 'px';
  15. return refreshRem;
  16. })();
  17. /* 添加倍屏标识,安卓倍屏为1 */
  18. docEl.setAttribute(
  19. 'data-dpr',
  20. window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1
  21. );
  22. if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {
  23. /* 添加IOS标识 */
  24. doc.documentElement.classList.add('ios');
  25. /* IOS8以上给html添加hairline样式,以便特殊处理 */
  26. if (parseInt(window.navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/)[1], 10) >= 8)
  27. doc.documentElement.classList.add('hairline');
  28. }
  29. if (!doc.addEventListener) return;
  30. window.addEventListener(resizeEvt, recalc, false);
  31. doc.addEventListener('DOMContentLoaded', recalc, false);
  32. })(window);