rem適配移動端

1、屏幕寬度 / 設計稿寬度 *100 來設置根元素的 font-size   10px = 0.10remhtml

(function (doc, win) {
    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            if(clientWidth>=640){
                docEl.style.fontSize = '100px';
            }else{
                docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
            }
        };

    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

 

2、函數

const MAX_FONT_SIZE = 42;

document.addEventListener('DOMContentLoaded', () => {
    const html = document.querySelector('html');
    let fontSize = window.innerWidth / 10;
    fontSize  = fontSize > MAX_FONT_SIZE ? MAX_FONT_SIZE : fontSize;
    html.style.fontSize = fontSize + 'px';
});
// 以 Iphone 六、6s、七、8 爲設計基礎。
// 定義預計根元素 fontSize。
$rootFontSize: 375 / 10;
// 定義像素轉化爲 rem 函數
@function px2rem ($px) {
    @return $px / $rootFontSize + rem;
}
相關文章
相關標籤/搜索