首頁  >  文章  >  web前端  >  js實作rem自動比對計算font-size

js實作rem自動比對計算font-size

小云云
小云云原創
2018-01-18 10:50:331494瀏覽

本文主要介紹了js實作rem自動比對計算font-size的範例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

實際開發過程中,我們常常會被各種寬度,高度計算搞暈。尤其是使用了rem的計算方式,自適應佈局難倒一大片程式設計師。為了解決這類問題,我覺得可以利用js監聽螢幕寬度變化來實現更改HTML 根元素font-size的值。

下面是相關JavaScript的實作程式碼:


(function (doc, win) {
  var docEl = doc.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function () {
      var clientWidth = docEl.clientWidth;
      if (!clientWidth) return;
      if(clientWidth>=640){
        docEl.style.fontSize = '100px';
      }else{
        docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
      }
    };
  if (!doc.addEventListener) return;
  win.addEventListener(resizeEvt, recalc, false);
  doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

此程式碼選640px為基準值,為什麼選640呢,

#640px的頁面寬度是一個安全的最大寬度,保證了行動版頁面兩邊不會留白。注意這裡的px是css邏輯像素,與裝置的實體像素是有差別的。如iPhone 5所使用的是Retina視網膜螢幕,使用2px x 2px的 device pixel 代表 1px x 1px 的 css pixel,所以裝置像素數為640 x 1136px,而它的CSS邏輯像素數為320 x 568px。
所以當要切割行動端的頁面的時候,就需要把效果圖寬度等比例縮放到640px。

例如當頁面中某一p的寬度為60,高度為65的時候,就可以直接這樣寫樣式:


{
  width:0.6rem;
  height:0.65rem
}

瀏覽器的相容性

rem是CSS3新引進的度量單位,大家心裡一定會覺得心灰意冷呀,擔心瀏覽器的支援狀況。其實大家不用害怕,你可能會驚訝,支援的瀏覽器還是蠻多的,例如:Mozilla Firefox 3.6+、Apple Safari 5+、Google Chrome、IE9+和Opera11+。只是可憐的IE6-8無法,你們就把他們當透明了吧,我向來都是如此。

不過使用單位設定字體,可不能完全不考慮IE了,如果你想使用這個REM,但也想相容IE下的效果,可你可考慮「px」和「rem」一起使用,用"px"來實現IE6-8下的效果,然後使用「Rem」來實現代瀏覽器的效果。就讓IE6-8不能隨文字的改變而改變吧,誰讓這個Ie6-8這麼老呢?大家不彷試試,還蠻有意思,說不定這個就是主流的度量單位了。

完整實例程式碼:


<!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>响应式布局</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>页面头部</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">页面底部</footer>
  <script>
    //orientationchange方向改变事件
    (function (doc, win) {
     var docEl = doc.documentElement,//根元素html
     //判断窗口有没有orientationchange这个方法,有就赋值给一个变量,没有就返回resize方法。
      resizeEvt = &#39;orientationchange&#39; in window ? &#39;orientationchange&#39; : &#39;resize&#39;,
      recalc = function () {
       var clientWidth = docEl.clientWidth;
       if (!clientWidth) return;
       //把document的fontSize大小设置成跟窗口成一定比例的大小,从而实现响应式效果。
       docEl.style.fontSize = 20 * (clientWidth / 320) + &#39;px&#39;;
      };
      //alert(docEl)
     if (!doc.addEventListener) return;
     win.addEventListener(resizeEvt, recalc, false);//addEventListener事件方法接受三个参数:第一个是事件名称比如点击事件onclick,第二个是要执行的函数,第三个是布尔值
     doc.addEventListener(&#39;DOMContentLoaded&#39;, recalc, false)//绑定浏览器缩放与加载时间
    })(document, window);
    //alert(document.documentElement.clientWidth/320)

  </script>
 </body>
</html>

相關推薦:

html5頁面rem佈局適配方法詳解

javascript用rem來做響應式開發實例分享

#rem絕對自適應方案解析

#

以上是js實作rem自動比對計算font-size的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn