在開發(fā)App端的網(wǎng)頁時(shí),要適配iphone、ipad、ipod、安卓等各種機(jī)型,一般是直接使用em、px轉(zhuǎn)em、界面縮放。
本章是通過將界面縮放,等比例顯示在各機(jī)型上。過程中遇到了些問題和大坑~
然后下面是具體的自適應(yīng)實(shí)現(xiàn)方式的嘗試~
首先設(shè)置內(nèi)容固定寬度、自動(dòng)高度(以下舉例)
width: 375px; height: auto;
通過獲取窗口的寬度與固定寬度相除,獲得縮放比例
const scaleValue=window.innerWidth / 375
在html層,添加一段script:
<script dangerouslySetInnerHTML={{ __html: this.getScript() }}></script>
添加一段設(shè)置zoom值的函數(shù):
getScript() { return ` const zoomValue=window.innerWidth / 375; document.documentElement.style.transform="scale("+zoomValue+")"; document.documentElement.style.transformOrigin="left top"; ; }
注:
以上也可以直接寫script,我上面返回一段html是因?yàn)轫?xiàng)目是通過服務(wù)端渲染的。
樣式的設(shè)置必須在界面加載之前,否則會(huì)因界面顯示變更出現(xiàn)閃現(xiàn)問題。
因?yàn)樘砑恿朔?wù)端渲染,所以無法在界面一開始初始時(shí),無法獲取window、document等對(duì)象。而上面html的注入,對(duì)服務(wù)端渲染機(jī)制的一個(gè)黑科技~
上面的方案完成后,看看效果。然后坑出來了:
getScript() {return `const zoomValue=window.innerWidth / 375;document.documentElement.style.zoom = zoomValue;;}
<meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1,user-scalable=no, minimal-ui"></meta>
getScript() { return `const zoomValue=window.innerWidth / 375;var viewport = document.querySelector("meta[name=viewport]");viewport.content="width=device-width,initial-scale="+zoomValue+", maximum-scale="+zoomValue+", minimum-scale="+zoomValue+",user-scalable=no, minimal-ui" ; }
{/* app contentAutoFit */} <script dangerouslySetInnerHTML={{ __html: this.getZoomScript() }}></script>
//返回自適應(yīng)html字符串 getZoomScript() { return ` const zoomValue = window.innerWidth / 375; const userAgentInfo = window.clientInformation.appVersion; //如果是ipad if (userAgentInfo.indexOf("iPad") != -1 || userAgentInfo.indexOf("Macintosh") != -1) { //內(nèi)容自適應(yīng) - 設(shè)置transform-scale。 //fixed布局時(shí)需要修改下left/margin-left等,同時(shí)窗口的寬高無法準(zhǔn)確獲取,需要除以比例,詳見windowSizeWithScaleHelper //ipad有遺留問題:微信瀏覽器加載時(shí),橫豎屏切換一瞬間,有空白問題。不過可以忽略~ document.documentElement.style.transform = "scale(" + zoomValue + "," + (zoomValue < 1 ? 1 : zoomValue) + ")"; document.documentElement.style.transformOrigin = "left top"; var html = document.querySelector("html"); html.style.width = '375px'; html.style.overflow = 'hidden'; html.style.overflowY = 'auto'; } else { //內(nèi)容自適應(yīng) - 設(shè)置zoom。通過zoom來縮放界面,在ipad的safari瀏覽器等會(huì)存在字體無法縮放的兼容問題。 document.documentElement.style.zoom = zoomValue; } // 內(nèi)容自適應(yīng) - 設(shè)置viewport,整體okay。但是ipad的水平滾動(dòng)條無法解決 // var viewport = document.querySelector("meta[name=viewport]"); // viewport.content = "width=device-width,initial-scale=" + zoomValue + ", maximum-scale=" + zoomValue + ", minimum-scale=" + zoomValue + ",user-scalable=no, minimal-ui" `; }
componentDidMount() {//window.onresize = this.adjustContentAutoFit;//解決微信橫豎屏問題window.addEventListener("orientationchange", this.adjustContentAutoFit);//解決加載過程中,切換橫豎屏,導(dǎo)致界面沒有適配的問題this.adjustContentAutoFit(); } componentWillUnmount() {window.removeEventListener("orientationchange", this.adjustContentAutoFit); }//監(jiān)聽窗口尺寸變更,刷新自適應(yīng) adjustContentAutoFit() {const zoomValue = window.innerWidth / 375;const userAgentInfo = window.clientInformation.appVersion;//如果是ipadif (userAgentInfo.indexOf("iPad") != -1 || userAgentInfo.indexOf("Macintosh") != -1) {//內(nèi)容自適應(yīng) - 設(shè)置transform-scale。//fixed布局時(shí)需要修改下left/margin-left等,同時(shí)窗口的寬高無法準(zhǔn)確獲取,需要除以比例,詳見windowSizeWithScaleHelper//ipad有遺留問題:微信瀏覽器,橫豎屏切換時(shí),有些機(jī)型在打開一瞬間,有空白問題。不過可以忽略~document.documentElement.style.transform = "scale(" + zoomValue + "," + (zoomValue < 1 ? 1 : zoomValue) + ")";document.documentElement.style.transformOrigin = "left top";var html = document.querySelector("html") as HTMLElement; html.style.width = '375px'; html.style.overflow = 'hidden'; html.style.overflowY = 'auto'; } else {// 內(nèi)容自適應(yīng) - 設(shè)置zoom。通過zoom來縮放界面,在ipad的safari瀏覽器等會(huì)存在字體無法縮放的兼容問題。document.documentElement.style.zoom = zoomValue; }// 內(nèi)容自適應(yīng) - 設(shè)置viewport,整體okay。但是ipad的水平滾動(dòng)條無法解決// var viewport = document.querySelector("meta[name=viewport]");// viewport.content = "width=device-width,initial-scale=" + zoomValue + ", maximum-scale=" + zoomValue + ", minimum-scale=" + zoomValue + ",user-scalable=no, minimal-ui" }
聯(lián)系客服
微信登錄中...
請(qǐng)勿關(guān)閉此頁面