js rem 单位适配(手机、平板、PC)?
生活随笔
收集整理的這篇文章主要介紹了
js rem 单位适配(手机、平板、PC)?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
js rem單位適配
<script type="text/javascript">// 設備區分 (安卓、火狐、平板、PC)var os = function() {var ua = navigator.userAgent,isAndroid = /(?:Android)/.test(ua),isFireFox = /(?:Firefox)/.test(ua),isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),isPC = !(/Android|webOS|iPhone|iPod|BlackBerry/i.test(ua));return {isTablet: isTablet,};}();(function (window, document) {function resize() {var docEl = document.documentElement;var clientWidth = docEl.clientWidth;var clientHeight = docEl.clientHeight;if (clientWidth < clientHeight) { // 手機豎屏docEl.style.fontSize = clientWidth / 7.5 + "px";} else { // 手機橫屏 docEl.style.fontSize = clientHeight / 7.5 + "px";}if(os.isTablet) { //ipad if(clientWidth >= 1366) {docEl.style.fontSize = clientWidth / 1366 * 100 + "px";} else {docEl.style.fontSize = clientWidth / 1024 * 100 + "px";}}if (os.isPC) { // pcif(clientWidth >= 1920) {docEl.style.fontSize = 1920 / 19.20 + "px";} else {docEl.style.fontSize = clientWidth / 19.20 + "px";}} }resize();// reset rem unit on page resizewindow.addEventListener("resize", function () {resize()});window.addEventListener('pageshow', function (e) {if (e.persisted) {resize()}});}(window, document)); </script>總結
以上是生活随笔為你收集整理的js rem 单位适配(手机、平板、PC)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css媒体查询(手机、平板、PC)
- 下一篇: C++基础知识(五)—— 基本输入输出