首先單位自適應,使用rem做單位,rem根據根標籤來決定,若html的font-size爲50px;那麼1rem就是50px;
然後我們在根據屏幕大小來給html定義font-size;把以下代碼寫到index.html中
fnResize()
window.onresize = function () {
fnResize()
}
function fnResize() {
var deviceWidth = document.documentElement.clientWidth || window.innerWidth
if (deviceWidth >= 750) {
deviceWidth = 750
}
if (deviceWidth <= 320) {
deviceWidth = 320
}
document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px'
}