rem适配的js

function pixel2rem() {
    var docEl = document.documentElement || document.body,
        isIOS = navigator.userAgent.match(/iphone|ipod|ipad/gi),
        dRatio = window.devicePixelRatio,
        dpr = isIOS && dRatio != undefined ? Math.min(dRatio, 3) : (dRatio != undefined ? dRatio : 1),  //设备像素比devicePixelRatio
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
    docEl.dataset.dpr = dpr;

    var recalc = function() {
        //页面内容可见区域的宽度(兼容各种平台),对于ios设备devicePixelRatio值只可能是1或者2
        //当width=device-width时,可视区的宽度就是document.documentElement.clientWidth
        //安卓平台下可见区的宽度:document.body.clientWidth || document.documentElement.clientWidth
        var width = docEl.clientWidth || window.innerWidth;
        if (width / dpr > 750) {
            width = 750 * dpr;
        }
        docEl.style.fontSize = 100 * (width / 750) + 'px';
    };
    recalc();
    if (!document.addEventListener) return;
    window.addEventListener(resizeEvt, recalc, false);
}

export default pixel2rem

在main.js中引用
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值