html5中如何使用hotcss.js實現(xiàn)手機端自適配

這篇文章將為大家詳細講解有關(guān)html5中如何使用hotcss.js實現(xiàn)手機端自適配,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注于夏河企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),成都做商城網(wǎng)站。夏河網(wǎng)站建設(shè)公司,為夏河等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

簡介

使用動態(tài)的HTML根字體大小和動態(tài)的viewport scale。

遵循視覺一致性原則。在不同大小的屏幕和不同的設(shè)備像素密度下,讓你的頁面看起來是一樣的。

1.新建一個項目文件夾,目錄結(jié)構(gòu)如下圖:

src //主要文件在這里
├── hotcss.js
├── px2rem.less
├── px2rem.scss
└── px2rem.styl

2.hotcss.js 文件可以下載官方的,也可以在大神GitHub(https://github.com/Grace110/hotcss)上下載整個demo

注意:

hotcss.js必須在其他JS加載前加載,萬不可異步加載。

如果可以,你應(yīng)將hotcss.js的內(nèi)容以內(nèi)嵌的方式寫到<head>標簽里面進行加載,并且保證在其他js文件之前。

為了避免不必要的bug,請將CSS放到該JS之前加載。

hotcss.js也可以直接復(fù)制到<head>標簽里面

<script>(function(window,document){var hotcss={};(function(){var viewportEl=document.querySelector('meta[name="viewport"]'),hotcssEl=document.querySelector('meta[name="hotcss"]'),dpr=window.devicePixelRatio||1,maxWidth=540,designWidth=0;dpr=dpr>=3?3:dpr>=2?2:1;if(hotcssEl){var hotcssCon=hotcssEl.getAttribute("content");if(hotcssCon){var initialDprMatch=hotcssCon.match(/initial\-dpr=([\d\.]+)/);if(initialDprMatch){dpr=parseFloat(initialDprMatch[1])}var maxWidthMatch=hotcssCon.match(/max\-width=([\d\.]+)/);if(maxWidthMatch){maxWidth=parseFloat(maxWidthMatch[1])}var designWidthMatch=hotcssCon.match(/design\-width=([\d\.]+)/);if(designWidthMatch){designWidth=parseFloat(designWidthMatch[1])}}}document.documentElement.setAttribute("data-dpr",dpr);hotcss.dpr=dpr;document.documentElement.setAttribute("max-width",maxWidth);hotcss.maxWidth=maxWidth;if(designWidth){document.documentElement.setAttribute("design-width",designWidth);hotcss.designWidth=designWidth}var scale=1/dpr,content="width=device-width, initial-scale="+scale+", minimum-scale="+scale+", maximum-scale="+scale+", user-scalable=no";if(viewportEl){viewportEl.setAttribute("content",content)}else{viewportEl=document.createElement("meta");viewportEl.setAttribute("name","viewport");viewportEl.setAttribute("content",content);document.head.appendChild(viewportEl)}})();hotcss.px2rem=function(px,designWidth){if(!designWidth){designWidth=parseInt(hotcss.designWidth,10)}return(parseInt(px,10)*320)/designWidth/20};hotcss.rem2px=function(rem,designWidth){if(!designWidth){designWidth=parseInt(hotcss.designWidth,10)}return(rem*20*designWidth)/320};hotcss.mresize=function(){var innerWidth=document.documentElement.getBoundingClientRect().width||window.innerWidth;if(hotcss.maxWidth&&innerWidth/hotcss.dpr>hotcss.maxWidth){innerWidth=hotcss.maxWidth*hotcss.dpr}if(!innerWidth){return false}document.documentElement.style.fontSize=(innerWidth*20)/320+"px";hotcss.callback&&hotcss.callback()};hotcss.mresize();window.addEventListener("resize",function(){clearTimeout(hotcss.tid);hotcss.tid=setTimeout(hotcss.mresize,33)},false);window.addEventListener("load",hotcss.mresize,false);setTimeout(function(){hotcss.mresize()},333);window.hotcss=hotcss})(window,document);</script>
 

//pc2rem.scss 頁面代碼

@function px2rem( $px ){
    @return $px*320/$designWidth/20 + rem;
}
$designWidth : 750; //如設(shè)計圖是750

3.但是html是無法直接調(diào)用scss文件的,這時我們需要一個 scss文件 實時編譯器

vscode 編輯器里面安裝插件

html5中如何使用hotcss.js實現(xiàn)手機端自適配

4.編寫代碼

寫個簡單的html頁面,內(nèi)容如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>hotcss在h6中的使用</title>
<!-- 引入hot.scss文件 ,或者把js文件直接復(fù)制到這里-->
    <script src="js/hotcss.js"></script>
    <!-- 引入css文件,注意,不是scss -->
    <link rel="stylesheet" href="css/style.css"> 
</head>
<body>
    <div class="container">
        <div class="content">
            <p>hotcss在h6中的使用</p>
        </div>
    </div>
</body>
 
</html>

接下來寫scss 樣式

html5中如何使用hotcss.js實現(xiàn)手機端自適配

同時打開style.css,可以看到,style.scss文件上的內(nèi)容會實時編譯到style.css'

html5中如何使用hotcss.js實現(xiàn)手機端自適配

走到這一步,就已經(jīng)能夠完成了自適應(yīng),在瀏覽器中打開

html5中如何使用hotcss.js實現(xiàn)手機端自適配

關(guān)于“html5中如何使用hotcss.js實現(xiàn)手機端自適配”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

當前標題:html5中如何使用hotcss.js實現(xiàn)手機端自適配
網(wǎng)頁URL:http://bm7419.com/article42/jdsoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、Google、微信公眾號品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)