js判斷移動端橫豎屏視口檢測實現的幾種方法
// 獲取視覺視口大?。òù怪睗L動條)let iw = window.innerWidth, ih = window.innerHeight;console.log(iw, ih);// 獲取視覺視口大小(內容區(qū)域大小,包括側邊欄、窗口鑲邊和調整窗口大小的邊框)let ow = window.outerWidth, oh = window.outerHeight;console.log(ow, oh);// 獲取屏幕理想視口大小,固定值(屏幕分辨率大?。﹍et 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);// 包括內邊距、滾動條、邊框和外邊距l(xiāng)et 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' />
設置安全區(qū)域與邊界的距離
/* 當使用底部固定導航欄時,我們要為他們設置 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 移動端橫豎屏視口檢測內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 編程語言PHP在Web開發(fā)領域的優(yōu)勢在哪?3. Spring Boot和Thymeleaf整合結合JPA實現分頁效果(實例代碼)4. Android如何加載Base64編碼格式圖片5. 什么是python的自省6. 解決Android studio xml界面無法預覽問題7. 基于android studio的layout的xml文件的創(chuàng)建方式8. 圖文詳解vue中proto文件的函數調用9. Springboot Druid 自定義加密數據庫密碼的幾種方案10. Vuex localStorage的具體使用

網公網安備