百度 前端 rem 适配 和 阿里 前端 rem 适配

阿里 

<script>
 var html = document.documentElement;
 window.rem = html.getBoundingClientRect().width / 37.5;
 html.style.fontSize = window.rem + 'px';
 </script>

阿里2 

<script>
 (function(doc, win) {
 var docEle = doc.documentElement,
 evt = "onorientationchange" in window ? "orientationchange" : "resize",
 fn = function() {
 var width = docEle.clientWidth;
 width && (docEle.style.fontSize = 10 * (width / 375) + "px")
 }
 win.addEventListener(evt, fn, false);
 doc.addEventListener("DOMContentLoaded", fn, false)
  
 }(document, window))
 </script>

 

百度

 (function () {
var newFontSize = window.getComputedStyle(document.documentElement)['font-size'];
var fontSizeScale = 16 / parseFloat(newFontSize.replace('px','')) ;
//获取屏幕大小
var html = document.documentElement;
document.addEventListener('DOMContentLoaded', function () {
resetRem();
resetScreen();
});
window.addEventListener('load', function () {
setTimeout(resetRem, 300);
resetScreen();
});
window.addEventListener('resize', function () {
setTimeout(resetRem, 300);
resetScreen();
});
function resetRem() {
html.style.fontSize = html.clientWidth / 20 * fontSizeScale + 'px';
console.log(html.clientWidth);
console.log(html.style.fontSize);
}
function resetScreen() {
window.scrollTo(0,0);
// var height = html.clientHeight || window.innerHeight;
// var width = html.clientWidth || window.innerWidth;
// document.querySelector('body').style.height = height + 'px';
// document.querySelector('body').style.width = width + 'px';
// document.querySelector('body').style.margin = 0;
// document.querySelector('body').style.padding = 0;
}
resetRem();
})();

转载于:https://www.cnblogs.com/itliulei/p/9223386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值