css和js设置rem

css媒体查询:

html{
    font-size: 88px;
}

@media screen and (min-width: 340px){
    html{
        font-size: 94px;
    }
}

@media screen and (min-width: 360px){
    html{
        font-size: 100px;
    }
}

@media screen and (min-width: 380px){
    html{
        font-size: 106px;
    }
}

@media screen and (min-width: 400px){
    html{
        font-size: 110px;
    }
}

@media screen and (min-width: 420px){
    html{
        font-size: 114px;
    }
}

@media screen and (min-width: 450px){
    html{
        font-size: 120px;
    }
}

js动态设置:

(function() {

     var newRem = function() {

     var html = document.documentElement;

     var ClientWidth = html.getBoundingClientRect().width;

        if( ClientWidth <= 640){
            html.style.fontSize = ClientWidth / 3.6 + 'px';
        }else{
             html.style.fontSize = 100px;
        }
    };

     window.addEventListener('resize', newRem, false);
     newRem();
})();

猜你喜欢

转载自blog.csdn.net/zzzyyc/article/details/80422255
今日推荐