HTML5中判斷橫屏豎屏 HTML5中判斷橫屏豎屏

此文轉載自:http://www.cnblogs.com/qqziyuan8/p/5735884.html,僅供本人學習參考,版權歸原做者全部!

HTML5中判斷橫屏豎屏

在移動端中咱們常常碰到橫屏豎屏的問題,那麼咱們應該如何去判斷或者針對橫屏、豎屏來寫不一樣的代碼呢。javascript

這裏有兩種方法:css

一:CSS判斷橫屏豎屏

寫在同一個CSS中html

1
2
3
4
5
6
@media  screen  and (orientation:  portrait ) {
   /*豎屏 css*/
}
@media  screen  and (orientation:  landscape ) {
   /*橫屏 css*/
}

分開寫在2個CSS中java

豎屏瀏覽器

1
< link  rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">

橫屏iphone

1
< link  rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">

二:JS判斷橫屏豎屏

1
2
3
4
5
6
7
8
9
10
//判斷手機橫豎屏狀態:
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 );
//移動端的瀏覽器通常都支持window.orientation這個參數,經過這個參數能夠判斷出手機是處在橫屏仍是豎屏狀態。

屏幕方向對應的window.orientation值:
ipad,iphone: 90 或 -90 橫屏
ipad,iphone: 0 或180 豎屏
Andriod:0 或180 橫屏
Andriod: 90 或 -90 豎屏post

相關文章
相關標籤/搜索