js判斷移動(dòng)端橫豎屏視口檢測(cè)實(shí)現(xiàn)的幾種方法
// 獲取視覺視口大小(包括垂直滾動(dòng)條)let iw = window.innerWidth, ih = window.innerHeight;console.log(iw, ih);// 獲取視覺視口大小(內(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簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理2. UDDI FAQs3. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案4. ASP常用日期格式化函數(shù) FormatDate()5. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法6. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享7. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能8. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲9. jsp文件下載功能實(shí)現(xiàn)代碼10. JSP之表單提交get和post的區(qū)別詳解及實(shí)例
