您当前的位置:首页 > 网站建设 > javascript
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax |

js判断移动端横竖屏视口检测实现的几种方法

51自学网 2022-05-02 21:35:55
  javascript

1、不同视口的获取方法

// 获取视觉视口大小(包括垂直滚动条)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 移动端横竖屏视口检测内容请搜索wanshiok.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持wanshiok.com!


React中Portals与错误边界处理实现
Element树形控件整合带图标的下拉菜单(tree+dropdown+input)
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1