移動端適配js

來源:互聯網
上載者:User

標籤:useragent   tor   regex   add   get   function   index   class   style   

移動端適配方案非常多,入行兩年來始終沒有一直使用一個標準,每個項目都按照業務需求來做適配

方案一:

最初聽李彥輝老師的課程使用rem 布局了一段時間,但是字型計算比較麻煩,也不太精確

方案二:

偶然在Q群裡一群友分享的 動態改變根節點字型的方案布局,頭部引用一個js。按照設計圖的像素寫rem,100px = 1rem 

//控制字型var calculate_size = function() {    var BASE_FONT_SIZE = 100;    var docEl = document.documentElement,        clientWidth = docEl.clientWidth;    if (!clientWidth) return;    docEl.style.fontSize = BASE_FONT_SIZE * (clientWidth / 640) + ‘px‘;        //此處可根據設計圖可以改變數值};// 如果瀏覽器不支援addEventListener,則中止if (document.addEventListener) {    var resizeEvt = ‘orientationchange‘ in window ? ‘orientationchange‘ : ‘resize‘;    window.addEventListener(resizeEvt, calculate_size, false);    document.addEventListener(‘DOMContentLoaded‘, calculate_size, false);    calculate_size();}

 

方案三:

按照iphone6 750 的設計稿尺寸 用px 單位 縮小2倍的寬度布局,高度固定。750px = 375px。這是17年做移動端 常用的方式

 

方案四:

大神給的一個js 適配方案,動態建立meta 標籤,動態改變 viewport 。單位是兩倍 1px = 2px 。這種方案還在試用中

//添加viewport-mate標籤縮放網頁。!function(userAgent) {    if(userAgent.match(/AppleWebKit.*Mobile.*/)) {        var screen_w = parseInt(window.screen.width),            scale = screen_w / 750;        if(/Android (\d+\.\d+)/.test(userAgent)) {            var version = parseFloat(RegExp.$1);            if(userAgent.indexOf("MX") > -1 && version >= 5) {                //alert(userAgent)                document.write(‘<meta name="viewport" content="width=750,minimum-scale = ‘ + scale + ‘, maximum-scale = ‘ + scale + ‘, target-densitydpi=device-dpi">‘);            } else {                document.write(version > 2.3 ? ‘<meta name="viewport" content="width=750, minimum-scale = ‘ + scale + ‘, maximum-scale = ‘ + scale + ‘, target-densitydpi=device-dpi">‘ : ‘<meta name="viewport" content="width=750, target-densitydpi=device-dpi">‘);            };        } else {            document.write(‘<meta name="viewport" content="width=750, user-scalable=no, target-densitydpi=device-dpi">‘);        }    }}(navigator.userAgent);

 

移動端適配js

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.