HTML5中判斷橫屏豎屏

來源:互聯網
上載者:User

標籤:

此文轉載自:http://www.cnblogs.com/qqziyuan8/p/5735884.html,僅供本人學習參考,著作權歸原作者所有!HTML5中判斷橫屏豎屏

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

這裡有兩種方法:

一:CSS判斷橫屏豎屏

寫在同一個CSS中

123456 @media screen and (orientation: portrait) {  /*豎屏 css*/}@media screen and (orientation: landscape) {  /*橫屏 css*/}

分開寫在2個CSS中

豎屏

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

橫屏

1 <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
二:JS判斷橫屏豎屏
12345678910 //判斷手機橫豎屏狀態: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 豎屏

HTML5中判斷橫屏豎屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.