實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)被各種寬度,高度計(jì)算搞暈。尤其是使用了rem的計(jì)算方式,自適應(yīng)布局難倒一大片程序員。為了解決這類問(wèn)題,我覺(jué)得可以利用js監(jiān)聽(tīng)屏幕寬度變化來(lái)實(shí)現(xiàn)更改HTML 根元素font-size的值。
下面是相關(guān)JavaScript的實(shí)現(xiàn)代碼:
此代碼選640px為基準(zhǔn)值,為什么選640呢,
640px的頁(yè)面寬度是一個(gè)安全的最大寬度,保證了移動(dòng)端頁(yè)面兩邊不會(huì)留白。注意這里的px是css邏輯像素,與設(shè)備的物理像素是有區(qū)別的。如iPhone 5使用的是Retina視網(wǎng)膜屏幕,使用2px x 2px的 device pixel 代表 1px x 1px 的 css pixel,所以設(shè)備像素?cái)?shù)為640 x 1136px,而它的CSS邏輯像素?cái)?shù)為320 x 568px。
所以當(dāng)要切移動(dòng)端的頁(yè)面的時(shí)候,需要把效果圖寬度等比例縮放到640px。
比如當(dāng)頁(yè)面中某一p的寬度為60,高度為65的時(shí)候,就可以直接這樣寫(xiě)樣式:
{ width:0.6rem; height:0.65rem }
瀏覽器的兼容性
rem是CSS3新引進(jìn)來(lái)的一個(gè)度量單位,大家心里肯定會(huì)覺(jué)得心灰意冷呀,擔(dān)心瀏覽器的支持情況。其實(shí)大家不用害怕,你可能會(huì)驚訝,支持的瀏覽器還是蠻多的,比如:Mozilla Firefox 3.6+、Apple Safari 5+、Google Chrome、IE9+和Opera11+。只是可憐的IE6-8無(wú)法,你們就把他們當(dāng)透明了吧,我向來(lái)都是如此。
不過(guò)使用單位設(shè)置字體,可不能完全不考慮IE了,如果你想使用這個(gè)REM,但也想兼容IE下的效果,可你可考慮“px”和“rem”一起使用,用"px"來(lái)實(shí)現(xiàn)IE6-8下的效果,然后使用“Rem”來(lái)實(shí)現(xiàn)代瀏覽器的效果。就讓IE6-8不能隨文字的改變而改變吧,誰(shuí)讓這個(gè)Ie6-8這么老呢?大家不仿試試,還蠻有意思,說(shuō)不定這個(gè)就是主流的度量單位了。
完整實(shí)例代碼:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" /> <meta content="telephone=no" name="format-detection" /> <meta name="format-detection" content="email=no" /> <meta http-equiv="Cache-Control" content="no-cache"/> <title>響應(yīng)式布局</title> <style> html{font-size: 20px;width: 100%;height: 100%;} body{margin: 0;padding: 0;} header,footer{width: 100%;background: #17A578;color: #fff;font-size:1rem;text-align: center;line-height: 2rem;} .footer{position: fixed;bottom: 0;} .box{} .public{width: 5rem;height: 5rem;font-size: 1.2rem;display: inline-block;text-align: center;color: #fff;line-height: 5rem;margin-top: 1rem;} .left{background: #f00;} .center{background: #048F74;} .right{background: #000;} </style> </head> <body> <header>頁(yè)面頭部</header> <p class="box"> <p class="public left">左</p> <p class="public center">中</p> <p class="public right">右</p> <p class="public left">左</p> <p class="public center">中</p> <p class="public right">右</p> </p> <footer class="footer">頁(yè)面底部</footer> <script> //orientationchange方向改變事件 (function (doc, win) { var docEl = doc.documentElement,//根元素html //判斷窗口有沒(méi)有orientationchange這個(gè)方法,有就賦值給一個(gè)變量,沒(méi)有就返回resize方法。 resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientWidth = docEl.clientWidth; if (!clientWidth) return; //把document的fontSize大小設(shè)置成跟窗口成一定比例的大小,從而實(shí)現(xiàn)響應(yīng)式效果。 docEl.style.fontSize = 20 * (clientWidth / 320) + 'px'; }; //alert(docEl) if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false);//addEventListener事件方法接受三個(gè)參數(shù):第一個(gè)是事件名稱比如點(diǎn)擊事件onclick,第二個(gè)是要執(zhí)行的函數(shù),第三個(gè)是布爾值 doc.addEventListener('DOMContentLoaded', recalc, false)//綁定瀏覽器縮放與加載時(shí)間 })(document, window); //alert(document.documentElement.clientWidth/320) </script> </body> </html>
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com