移動端判斷手機橫豎屏狀態

來源:互聯網
上載者:User

標籤:不同   on()   樣式   dcl   func   OLE   div   預設   strong   

禁用使用者自動縮放功能:<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> 判斷橫豎屏狀態有兩種方法:css判斷、js判斷(一)、css判斷橫屏還是豎屏1、寫在同一個css檔案中
@media screen and (orientation: portrait) {  /*豎屏 css*/} @media screen and (orientation: landscape) {  /*橫屏 css*/}

根據橫豎屏設定大小時,正常頁面尺寸常規書寫即可,橫屏樣式單獨設定。即只需在原有樣式基礎上添加橫屏樣式即可,

@media screen and (orientation: landscape) {  /*橫屏 css*/}
當使用者橫屏時載入橫屏樣式,豎屏時取消橫屏樣式即載入預設樣式。

2、分開寫在兩個css中,根據橫豎屏引用不同樣式檔案:

    根據橫豎屏引用不同樣式檔案

橫屏:

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

豎屏:

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

備忘:css判斷橫豎屏是時時的可無縫銜接,即橫屏載入橫屏樣式,豎屏載入豎屏樣式,

(二)js判斷橫屏還是豎屏
//判斷手機橫豎屏狀態:    window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {        if (window.orientation === 180 || window.orientation === 0) {             console.log("豎屏");            $(".codeIg_s").removeClass(‘vercreen‘);//取消橫屏樣式        }         if (window.orientation === 90 || window.orientation === -90 ){             console.log("橫屏");            $(".codeIg_s").addClass(‘vercreen‘);//添加橫屏樣式        }      }, false); 

備忘:橫屏樣式在vercreen類名下修改原預設樣式,可正常使用,缺點是必須有橫豎屏的切換狀態才會觸發。

 

如果使用者預設是橫屏狀態時不會觸發橫屏條件判斷,只有使用者從橫屏轉為豎屏或者從豎屏轉為橫屏時才會觸發相應條件判斷。

沒有切換狀態時會不執行任何條件判斷,此處不如css判斷橫豎屏全面。

 

移動端判斷手機橫豎屏狀態

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.