移動端屏幕自適應(筆記)

首先單位自適應,使用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'
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章