移动端屏幕自适应(笔记)

it2022-05-05  186

首先单位自适应,使用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' }

最新回复(0)