判断横屏竖屏(三种)
2019/6/27 21:10:22
本文主要是介绍判断横屏竖屏(三种),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在做移动端页面的时候经常会遇到需要判断横屏还是竖屏。下面将目前已知的通过HTML,CSS,JS三种判断方法记录下来,方便以后翻阅。
1、通过在html中分别引用横屏和竖屏的样式:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css" rel="external nofollow" > //引用竖屏的CSS <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" rel="external nofollow" > //引用横屏的CSS
2、CSS中通过媒体查询的方法来判断:
@media (orientation: portrait ){ //竖屏CSS } @media ( orientation: landscape ){ //横屏CSS }
3、js判断是否为横屏竖屏:
window.addEventListener("onorientationchange" in window ? orientationchange" : "resize", function() { if (window.orientation === 180 || window.orientation === 0) { alert('竖屏状态!'); } if (window.orientation === 90 || window.orientation === -90 ){ alert('横屏状态!'); } }, false);
只要用户改变了设备的查看模式,就会触发onorientationchange事件。
orientation有4个值:0,90,-90,180
值为0和180的时候为竖屏(180为倒过来的竖屏);
90和-90时为横屏(-90为倒过来的竖屏模式);
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持找一找教程网!
这篇关于判断横屏竖屏(三种)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南