1. 程式人生 > >手機端自適應(rem使用)

手機端自適應(rem使用)

<!DOCTYPE html>
<html lang="en" style="font-size: 100px">
<head>
    <!--使用 <meta> 元素來描述HTML文件的描述,關鍵詞,作者,字符集等。-->
    <meta charset="UTF-8">
    <!--H5頁面視窗自動調整到裝置寬度,並禁止使用者縮放頁面-->
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<!--keywords用來告訴搜尋引擎你網頁的關鍵字是什麼--> <meta name="keywords" content="meta總結,html meta,meta屬性,meta跳轉"> <!--description用來告訴搜尋引擎你的網站主要內容--> <meta name="description" content="haorooms部落格,html的meta總結,meta是html語言head區的一個輔助性標籤。"> <!--標註網頁的作者--> <meta
name="author" content="root,[email protected]">
<!--可以用於設定網頁的到期時間。一旦網頁過期,必須到伺服器上重新傳輸。--> <meta http-equiv="expires" content="Fri,12Jan200118:18:18GMT"> <!--禁止瀏覽器從本地計算機的快取中訪問頁面內容。--> <meta http-equiv="Pragma" content="no-cache"> <!--忽略將頁面中的數字識別為電話號碼-->
<meta name="format-detection" content="telephone=no"/> <!--忽略Android平臺中對郵箱地址的識別--> <meta name="format-detection" content="email=no"/> <!--將網站新增到主螢幕快速啟動方式,僅針對ios的safari頂端狀態條的樣式--> <meta name="apple-mobile-web-app-status-bar-style" content="black"/> <!--使用 <title> 標籤定義HTML文件的標題--> <title>程式碼時代 CodeEra</title> <!--尺寸自適應--> <script> /*讓文字和標籤的大小隨著螢幕的尺寸做變話 等比縮放*/ var html = document.getElementsByTagName('html')[0]; console.log(html); /*取到螢幕的寬度*/ var width = window.innerWidth; console.log(width); /* 640 100 320 50 */ var fontSize = 100 / 640/*640是設計圖的尺寸*/ * width; console.log(fontSize); /*設定fontsize*/ html.style.fontSize = fontSize + 'px'; window.onresize = function () { var html = document.getElementsByTagName('html')[0]; console.log(html); /*取到螢幕的寬度*/ var width = window.innerWidth; console.log(width); /* 640 100 320 50 */ var fontSize = 100 / 640 * width; console.log(fontSize); /*設定fontsize*/ html.style.fontSize = fontSize + 'px'; } </script> <!--<link> 標籤定義了文件與外部資源之間的關係。 <link> 標籤通常用於連結到樣式表:--> <!--<style> 標籤定義了HTML文件的樣式檔案引用地址. 在<style> 元素中你也可以直接新增樣式來渲染 HTML 文件:--> <style> div { width: 2rem; height: 2rem; background-color: red; text-align: center; font-size: 0.5rem; line-height: 2rem; color: white; } </style> </head> <body> <div>自適應</div> <script language=JavaScript> document.write("最後更新時間: " + document.lastModified + "") </script> <noscript>抱歉,你的瀏覽器不支援 JavaScript!</noscript> </body> </html>