溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-03-20 09:53:44 來(lái)源:億速云 閱讀:170 作者:小新 欄目:web開(kāi)發(fā)

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

簡(jiǎn)介

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

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

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

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

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

注意:

hotcss.js必須在其他JS加載前加載,萬(wàn)不可異步加載。

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

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

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

<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 頁(yè)面代碼

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

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

vscode 編輯器里面安裝插件

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

4.編寫(xiě)代碼

寫(xiě)個(gè)簡(jiǎn)單的html頁(yè)面,內(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>

接下來(lái)寫(xiě)scss 樣式

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

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

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

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

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

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI