- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- js判斷移動(dòng)端橫豎屏視口檢測實(shí)現的幾種方法
// 獲取視覺(jué)視口大?。òù怪睗L動(dòng)條) let iw = window.innerWidth, ih = window.innerHeight; console.log(iw, ih); // 獲取視覺(jué)視口大?。▋热輩^域大小,包括側邊欄、窗口鑲邊和調整窗口大小的邊框) let ow = window.outerWidth, oh = window.outerHeight; console.log(ow, oh); // 獲取屏幕理想視口大小,固定值(屏幕分辨率大?。? let sw = window.screen.width, sh = window.screen.height; console.log(sw, sh); // 獲取瀏覽器可用窗口的大?。ò▋冗吘?、但不包括垂直滾動(dòng)條、邊框和外邊距) let aw = window.screen.availWidth, ah = window.screen.availHeight; console.log(aw, ah); // 包括內邊距、滾動(dòng)條、邊框和外邊距 let dow = document.documentElement.offsetWidth, doh = document.documentElement.offsetHeight; console.log(dow, doh); // 在不使用滾動(dòng)條的情況下適合視口中的所有內容所需的最小寬度和高度 let dsW = document.documentElement.scrollWidth, dsH = document.documentElement.scrollHeight; console.log(dsW, dsH); // 包含元素的內邊距,但不包括邊框、外邊距或者垂直滾動(dòng)條 let cw = document.documentElement.clientWidth, ch = document.documentElement.clientHeight; console.log(cw, ch);
// window.orientation:獲取屏幕旋轉方向 window.addEventListener('resize', () => { // 正常方向或屏幕旋轉180度 if (window.orientation === 180 || window.orientation === 0) { console.log('豎屏') } // 屏幕順時(shí)鐘旋轉90度或屏幕逆時(shí)針旋轉90度 if (window.orientation === 90 || window.orientation === -90) { console.log('橫屏') } });
/* 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; } }
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="viewport" content="viewport-fit=cover" />
設置安全區域與邊界的距離
/* 當使用底部固定導航欄時(shí),我們要為他們設置 padding值: */ body { padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom); }
注:constant 函數在iOS < 11.2時(shí)生效,env 在iOS >= 11.2時(shí)生效
到此這篇關(guān)于js判斷移動(dòng)端橫豎屏視口檢測實(shí)現的幾種方法的文章就介紹到這了,更多相關(guān)js 移動(dòng)端橫豎屏視口檢測內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站