首页 > 移动平台 > 详细

『REM』手机屏幕适配

时间:2018-03-30 19:15:17      阅读:180      评论:0      收藏:0      [点我收藏+]
function adapt(designWidth, rem2px){

  var d = window.document.createElement(‘div‘);

  d.style.width = ‘1rem‘;

  d.style.display = "none";

  var head = window.document.getElementsByTagName(‘head‘)[0];

  head.appendChild(d);

  var defaultFontSize = parseFloat(window.getComputedStyle(d, null).getPropertyValue(‘width‘));

  d.remove();

  document.documentElement.style.fontSize = window.innerWidth / designWidth * rem2px / defaultFontSize * 100 + ‘%‘;

  var st = document.createElement(‘style‘);

  var portrait = "@media screen and (min-width: "+window.innerWidth+"px) {html{font-size:"+((window.innerWidth/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}";

  var landscape = "@media screen and (min-width: "+window.innerHeight+"px) {html{font-size:"+((window.innerHeight/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}"

  st.innerHTML = portrait + landscape;

  head.appendChild(st);

  return defaultFontSize

};

var defaultFontSize = adapt(640, 100);

  转载地址:http://mp.weixin.qq.com/s/JIRN56acxvJgmzk1Fjj6LA

『REM』手机屏幕适配

原文:https://www.cnblogs.com/zhoubingyan/p/8677758.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!