经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
移动端横竖屏检测
来源:cnblogs  作者:下落香樟树  时间:2019/9/23 8:50:55  对本文有异议

1、不同视口的获取方法

  1. // 获取视觉视口大小(包括垂直滚动条)
  2. let iw = window.innerWidth,
  3. ih = window.innerHeight;
  4. console.log(iw, ih);
  5. // 获取视觉视口大小(内容区域大小,包括侧边栏、窗口镶边和调整窗口大小的边框)
  6. let ow = window.outerWidth,
  7. oh = window.outerHeight;
  8. console.log(ow, oh);
  9. // 获取屏幕理想视口大小,固定值(屏幕分辨率大小)
  10. let sw = window.screen.width,
  11. sh = window.screen.height;
  12. console.log(sw, sh);
  13. // 获取浏览器可用窗口的大小(包括内边距、但不包括垂直滚动条、边框和外边距)
  14. let aw = window.screen.availWidth,
  15. ah = window.screen.availHeight;
  16. console.log(aw, ah);
  17. // 包括内边距、滚动条、边框和外边距
  18. let dow = document.documentElement.offsetWidth,
  19. doh = document.documentElement.offsetHeight;
  20. console.log(dow, doh);
  21. // 在不使用滚动条的情况下适合视口中的所有内容所需的最小宽度和高度
  22. let dsW = document.documentElement.scrollWidth,
  23. dsH = document.documentElement.scrollHeight;
  24. console.log(dsW, dsH);
  25. // 包含元素的内边距,但不包括边框、外边距或者垂直滚动条
  26. let cw = document.documentElement.clientWidth,
  27. ch = document.documentElement.clientHeight;
  28. console.log(cw, ch);

2、JavaScript检测横竖屏

  1. // window.orientation:获取屏幕旋转方向
  2. window.addEventListener('resize', () => {
  3. // 正常方向或屏幕旋转180度
  4. if (window.orientation === 180 || window.orientation === 0) {
  5. console.log('竖屏')
  6. }
  7. // 屏幕顺时钟旋转90度或屏幕逆时针旋转90度
  8. if (window.orientation === 90 || window.orientation === -90) {
  9. console.log('横屏')
  10. }
  11. });

3、CSS检测横竖屏

  1. /* css检测横竖屏 */
  2. @media screen and (orientation:portrait) {
  3. /* 竖屏 */
  4. #app {
  5. width: 100vw;
  6. height: 100vh;
  7. background: red;
  8. }
  9. }
  10. @media screen and (orientation:landscape) {
  11. /* 横屏 */
  12. #app {
  13. width: 50vw;
  14. height: 100vh;
  15. background: green;
  16. }
  17. }

4、meta标签属性设置

  1. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

5、meta标签属性设置设置刘海屏&底部小黑条

  1. <meta name="viewport" content="viewport-fit=cover" />

设置安全区域与边界的距离

  1. /* 当使用底部固定导航栏时,我们要为他们设置 padding值: */
  2. body {
  3. padding-bottom: constant(safe-area-inset-bottom);
  4. padding-bottom: env(safe-area-inset-bottom);
  5. }

注:constant函数在iOS<11.2时生效,env在iOS>=11.2时生效

原文链接:http://www.cnblogs.com/zxk5211/p/web_15.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号