HTML5中判斷橫屏豎屏javascript
在移動端中咱們常常碰到橫屏豎屏的問題,那麼咱們應該如何去判斷或者針對橫屏、豎屏來寫不一樣的代碼呢。css
這裏有兩種方法:html
寫在同一個CSS中 java
1
2
3
4
5
6
|
@media
screen
and (orientation:
portrait
) {
/*豎屏 css*/
}
@media
screen
and (orientation:
landscape
) {
/*橫屏 css*/
}
|
分開寫在2個CSS中post
豎屏url
1
|
<
link
rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
|
橫屏spa
1
|
<
link
rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
|
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(
'橫屏狀態!'
);
|