在挪动端中大家常常碰到横屏竖屏的难题,那末大家应当怎样去分辨或对于横屏、竖屏来写不一样的编码呢。
这里有两种方式:
1:CSS分辨横屏竖屏
写在同1个CSS中
XML/HTML Code拷贝內容到剪贴板
- @media screen and (orientation: portrait) {
- /*竖屏 css*/
- }
- @media screen and (orientation: landscape) {
- /*横屏 css*/
- }
分开写在2个CSS中
竖屏
XML/HTML Code拷贝內容到剪贴板
- <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
横屏
XML/HTML Code拷贝內容到剪贴板
- <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
2:JS分辨横屏竖屏
XML/HTML Code拷贝內容到剪贴板
- //分辨手机上横纵屏情况:
- window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
- if (window.orientation === 180 || window.orientation === 0) {
- alert('竖屏情况!');
- }
- if (window.orientation === 90 || window.orientation === ⑼0 ){
- alert('横屏情况!');
- }
- }, false);
//挪动端访问器1般都适用window.orientation这个主要参数,根据这个主要参数能够分辨出手机上是处在横屏還是竖屏情况。
显示屏方位对应的window.orientation值:
ipad,iphone: 90 或 ⑼0 横屏
ipad,iphone: 0 或180 竖屏
Andriod:0 或180 横屏
Andriod: 90 或 ⑼0 竖屏