rem開發移動端

關於開發移動端有很多的方式,用的最多的單位是rem,rem單位是自適應單位,開發一套代碼,可以適配不同的手機端,但是往往rem針對新手用的不熟悉,也不知道怎麼用,今天就介紹一種簡單的方式;

前提開發環境是在Visual Studio Code上。

!function(e,t){function n(){t.body?t.body.style.fontSize=12*o+"px":t.addEventListener("DOMContentLoaded",n)}function d(){var e=i.clientWidth/10;i.style.fontSize=e+"px"}var i=t.documentElement,o=e.devicePixelRatio||1;if(n(),d(),e.addEventListener("resize",d),e.addEventListener("pageshow",function(e){e.persisted&&d()}),o>=2){var a=t.createElement("body"),s=t.createElement("div");s.style.border=".5px solid transparent",a.appendChild(s),i.appendChild(a),1===s.offsetHeight&&i.classList.add("hairlines"),i.removeChild(a)}}(window,document);

這段代碼引入到公共js中,不需要設置html的字體大小,再把頭部meta的viewport標籤刪掉,在開發工具裏面下載一個cssrem的插件就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章