js判斷移動(dòng)端橫豎屏視口檢測(cè)實(shí)現(xiàn)的幾種方法
// 獲取視覺(jué)視口大小(包括垂直滾動(dòng)條)let iw = window.innerWidth, ih = window.innerHeight;console.log(iw, ih);// 獲取視覺(jué)視口大小(內(nèi)容區(qū)域大小,包括側(cè)邊欄、窗口鑲邊和調(diào)整窗口大小的邊框)let ow = window.outerWidth, oh = window.outerHeight;console.log(ow, oh);// 獲取屏幕理想視口大小,固定值(屏幕分辨率大小)let sw = window.screen.width, sh = window.screen.height;console.log(sw, sh);// 獲取瀏覽器可用窗口的大小(包括內(nèi)邊距、但不包括垂直滾動(dòng)條、邊框和外邊距)let aw = window.screen.availWidth, ah = window.screen.availHeight;console.log(aw, ah);// 包括內(nèi)邊距、滾動(dòng)條、邊框和外邊距l(xiāng)et dow = document.documentElement.offsetWidth, doh = document.documentElement.offsetHeight;console.log(dow, doh);// 在不使用滾動(dòng)條的情況下適合視口中的所有內(nèi)容所需的最小寬度和高度let dsW = document.documentElement.scrollWidth, dsH = document.documentElement.scrollHeight;console.log(dsW, dsH);// 包含元素的內(nèi)邊距,但不包括邊框、外邊距或者垂直滾動(dòng)條let cw = document.documentElement.clientWidth, ch = document.documentElement.clientHeight;console.log(cw, ch);2、JavaScript檢測(cè)橫豎屏
// window.orientation:獲取屏幕旋轉(zhuǎn)方向window.addEventListener(’resize’, () => { // 正常方向或屏幕旋轉(zhuǎn)180度 if (window.orientation === 180 || window.orientation === 0) { console.log(’豎屏’) } // 屏幕順時(shí)鐘旋轉(zhuǎn)90度或屏幕逆時(shí)針旋轉(zhuǎn)90度 if (window.orientation === 90 || window.orientation === -90) { console.log(’橫屏’) }});3、CSS檢測(cè)橫豎屏
/* css檢測(cè)橫豎屏 */@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標(biāo)簽屬性設(shè)置
<meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' />5、meta標(biāo)簽屬性設(shè)置設(shè)置劉海屏&底部小黑條
<meta name='viewport' content='viewport-fit=cover' />
設(shè)置安全區(qū)域與邊界的距離
/* 當(dāng)使用底部固定導(dǎo)航欄時(shí),我們要為他們?cè)O(shè)置 padding值: */body { padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom);}
注:constant 函數(shù)在iOS < 11.2時(shí)生效,env 在iOS >= 11.2時(shí)生效
到此這篇關(guān)于js判斷移動(dòng)端橫豎屏視口檢測(cè)實(shí)現(xiàn)的幾種方法的文章就介紹到這了,更多相關(guān)js 移動(dòng)端橫豎屏視口檢測(cè)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML 取得元素的字符數(shù)據(jù)2. 利用CSS3新特性創(chuàng)建透明邊框三角3. XML入門(mén)的常見(jiàn)問(wèn)題(三)4. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例5. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)6. 不要在HTML中濫用div7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程9. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼10. 詳解CSS偽元素的妙用單標(biāo)簽之美
