js判斷移動端橫豎屏視口檢測實現的幾種方法
// 獲取視覺視口大小(包括垂直滾動條)let iw = window.innerWidth, ih = window.innerHeight;console.log(iw, ih);// 獲取視覺視口大小(內容區域大小,包括側邊欄、窗口鑲邊和調整窗口大小的邊框)let ow = window.outerWidth, oh = window.outerHeight;console.log(ow, oh);// 獲取屏幕理想視口大小,固定值(屏幕分辨率大小)let sw = window.screen.width, sh = window.screen.height;console.log(sw, sh);// 獲取瀏覽器可用窗口的大小(包括內邊距、但不包括垂直滾動條、邊框和外邊距)let aw = window.screen.availWidth, ah = window.screen.availHeight;console.log(aw, ah);// 包括內邊距、滾動條、邊框和外邊距let dow = document.documentElement.offsetWidth, doh = document.documentElement.offsetHeight;console.log(dow, doh);// 在不使用滾動條的情況下適合視口中的所有內容所需的最小寬度和高度let dsW = document.documentElement.scrollWidth, dsH = document.documentElement.scrollHeight;console.log(dsW, dsH);// 包含元素的內邊距,但不包括邊框、外邊距或者垂直滾動條let cw = document.documentElement.clientWidth, ch = document.documentElement.clientHeight;console.log(cw, ch);2、JavaScript檢測橫豎屏
// window.orientation:獲取屏幕旋轉方向window.addEventListener(’resize’, () => { // 正常方向或屏幕旋轉180度 if (window.orientation === 180 || window.orientation === 0) { console.log(’豎屏’) } // 屏幕順時鐘旋轉90度或屏幕逆時針旋轉90度 if (window.orientation === 90 || window.orientation === -90) { console.log(’橫屏’) }});3、CSS檢測橫豎屏
/* css檢測橫豎屏 */@media screen and (orientation:portrait) { /* 豎屏 */ #app { width: 100vw; height: 100vh; background: red; }}@media screen and (orientation:landscape) { /* 橫屏 */ #app { width: 50vw; height: 100vh; background: green; }}4、meta標簽屬性設置
<meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' />5、meta標簽屬性設置設置劉海屏&底部小黑條
<meta name='viewport' content='viewport-fit=cover' />
設置安全區域與邊界的距離
/* 當使用底部固定導航欄時,我們要為他們設置 padding值: */body { padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom);}
注:constant 函數在iOS < 11.2時生效,env 在iOS >= 11.2時生效
到此這篇關于js判斷移動端橫豎屏視口檢測實現的幾種方法的文章就介紹到這了,更多相關js 移動端橫豎屏視口檢測內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
