最新黑馬java十次方社交專案教程
阿新 • • 發佈:2018-12-21
使用js+viewport動態設定手動適配rem
我的編輯器是vscode,添加了外掛cssrem自動轉換
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- <meta name="viewport" content="width=device-width,initial-scale=1.0"> --> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" /> <!-- 啟用360瀏覽器的極速模式(webkit) --> <meta name="renderer" content="webkit"> <!-- 避免IE使用相容模式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- 針對手持裝置優化,主要是針對一些老的不識別viewport的瀏覽器,比如黑莓 --> <meta name="HandheldFriendly" content="true"> <!-- 微軟的老式瀏覽器 --> <meta name="MobileOptimized" content="320"> <!-- uc強制豎屏 --> <meta name="screen-orientation" content="portrait"> <!-- QQ強制豎屏 --> <meta name="x5-orientation" content="portrait"> <!-- UC強制全屏 --> <meta name="full-screen" content="yes"> <!-- QQ強制全屏 --> <meta name="x5-fullscreen" content="true"> <!-- UC應用模式 --> <meta name="browsermode" content="application"> <!-- QQ應用模式 --> <meta name="x5-page-mode" content="app"> <!-- windows phone 點選無高光 --> <meta name="msapplication-tap-highlight" content="no"> <meta content="telephone=no" name="format-detection" /> <meta name="huaban" content="nopin" /> <link rel="icon" type="image/x-icon" href="/favicon.ico"> <title>新茶飲</title> <script src="/config.js"></script> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> </head> <body> <div id="app"></div> <!-- 在iphone 5 中1rem=16px; html font-size =16px=1rem; --> <script> //得到手機螢幕的寬度 let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth; console.log('htmlWidth',htmlWidth) //得到html的Dom元素 let htmlDom = document.getElementsByTagName('html')[0]; // if(htmlWidth>640){//超過640大小的,字型根部都是16px // htmlWidth=640; // console.log('htmlWidth-true',htmlWidth) // } //設定根元素字型大小 htmlDom.style.fontSize = htmlWidth / 40 + 'px'; </script> </body> </html>
根據css的媒體查詢動態設定根部html字型大小
html {font-size: 625%; /*100 ÷ 16 × 100% = 625%*/} @media screen and (min-width:360px) and (max-width:374px) and (orientation:portrait) { html { font-size: 703%; } } @media screen and (min-width:375px) and (max-width:383px) and (orientation:portrait) { html { font-size: 732.4%; } } @media screen and (min-width:384px) and (max-width:399px) and (orientation:portrait) { html { font-size: 750%; } } @media screen and (min-width:400px) and (max-width:413px) and (orientation:portrait) { html { font-size: 781.25%; } } @media screen and (min-width:414px) and (max-width:431px) and (orientation:portrait){ html { font-size: 808.6%; } } @media screen and (min-width:432px) and (max-width:479px) and (orientation:portrait){ html { font-size: 843.75%; } }