「關注 前端開發社區 ,回覆「 1」 便可加入 前端技術交流羣,回覆 「 2」 便可免費領取500G前端乾貨!javascript

方案一:設置tranform/scale
width: 375px; height: auto;
const scaleValue=window.innerWidth / 375
<script dangerouslySetInnerHTML={{ __html: this.getScript() }}></script>
getScript() { return ` const zoomValue=window.innerWidth / 375; document.documentElement.style.transform="scale("+zoomValue+")"; document.documentElement.style.transformOrigin="left top"; ; }
項目設置的absolue元素width 100%失效了 -- 能夠設置固定的寬度解決css
彈框position=fixed位置飛到天邊去了 -- 這個沒法規避。html
transform限制position:fixed的跟隨效果前端
關於在transform下的子元素設置fixed無效的困惑java
position:fixed不支持,因此想作標題欄置頂,上面方案是沒法實現的。ios
ipad有遺留問題:微信瀏覽器,橫豎屏切換時,有些機型在打開一瞬間,橫向拖動有空白問題。這個問題沒法處理~瀏覽器
以上方案由於使用了scale,同時窗口的寬高window.innerHeight沒法準確獲取,須要除以比例,好比: window.innerHeight / (window.innerWidth / 375)微信
方案二:設置zoom
getScript() {return `const zoomValue=window.innerWidth / 375;document.documentElement.style.zoom = zoomValue;;}
方案三:設置viewport-scare
<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" ; }
margin:auto,在某些佈局下會讓頁面偏移 -- 刪除就好app
設置background-image的區域,背景圖片並無填充滿 -- 添加width:100%解決iphone
position:fixed,寬高顯示有問題 -- 設置固定寬度,好比375px,固定高度;若是須要全屏,可使用height: 100vh。
以上方案不支持fixed佈局,修改完成後,ipad的水平滾動條依然存在,沒法解決
兼容適配
{/* app contentAutoFit */} <script dangerouslySetInnerHTML={{ __html: this.getZoomScript() }}></script>
//返回自適應html字符串 getZoomScript() { return ` const zoomValue = window.innerWidth / 375; const userAgentInfo = window.clientInformation.appVersion; //若是是ipad if (userAgentInfo.indexOf("iPad") != -1 || userAgentInfo.indexOf("Macintosh") != -1) { //內容自適應 - 設置transform-scale。 //fixed佈局時須要修改下left/margin-left等,同時窗口的寬高沒法準確獲取,須要除以比例,詳見windowSizeWithScaleHelper //ipad有遺留問題:微信瀏覽器加載時,橫豎屏切換一瞬間,有空白問題。不過能夠忽略~ 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 { //內容自適應 - 設置zoom。經過zoom來縮放界面,在ipad的safari瀏覽器等會存在字體沒法縮放的兼容問題。 document.documentElement.style.zoom = zoomValue; } // 內容自適應 - 設置viewport,總體okay。可是ipad的水平滾動條沒法解決 // 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" `; }
微信瀏覽器橫豎屏切換時,某些機型不會觸發窗口大小變動,因此須要額外添加orientationchange監聽
加載過程當中,微信瀏覽器切換橫豎屏會有顯示問題,須要加載完成後適配
componentDidMount() { //window.onresize = this.adjustContentAutoFit; //解決微信橫豎屏問題 window.addEventListener("orientationchange", this.adjustContentAutoFit); //解決加載過程當中,切換橫豎屏,致使界面沒有適配的問題 this.adjustContentAutoFit(); } componentWillUnmount() { window.removeEventListener("orientationchange", this.adjustContentAutoFit); } //監聽窗口尺寸變動,刷新自適應 adjustContentAutoFit() { const zoomValue = window.innerWidth / 375; const userAgentInfo = window.clientInformation.appVersion; //若是是ipad if (userAgentInfo.indexOf("iPad") != -1 || userAgentInfo.indexOf("Macintosh") != -1) { //內容自適應 - 設置transform-scale。 //fixed佈局時須要修改下left/margin-left等,同時窗口的寬高沒法準確獲取,須要除以比例,詳見windowSizeWithScaleHelper //ipad有遺留問題:微信瀏覽器,橫豎屏切換時,有些機型在打開一瞬間,有空白問題。不過能夠忽略~ 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 { // 內容自適應 - 設置zoom。經過zoom來縮放界面,在ipad的safari瀏覽器等會存在字體沒法縮放的兼容問題。 document.documentElement.style.zoom = zoomValue; } // 內容自適應 - 設置viewport,總體okay。可是ipad的水平滾動條沒法解決 // 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" }
ipad不支持position:fixed,因此沒法實現標題欄置頂等功能
微信瀏覽器,橫豎屏切換時,有些機型在打開一瞬間,有空白問題
IOS環境下固定定位position:fixed帶來的問題與解決方案
小技巧css解決移動端ios不兼容position:fixed屬性,無需插件
踩坑路上——IOS Safari瀏覽器下固定定位position:fixed帶來的問題與解決方案
iphone safari不支持position fixed的解決辦法
orientationchange事件、監測微信移動端橫豎屏

請各位帥哥美女多多支持帥編,回覆 「 1」 便可加入 前端技術交流羣 ,回覆 「 2」 便可領取500G前端乾貨
本文分享自微信公衆號 - 前端開發社區(pt1173179243)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。