Js/css detect changes in the direction of mobile devices judging the screen

Source: Internet
Author: User

Recently done the site is to be able to access the Computer Browser, ipad and can access, the ipad is divided into several, and the difference between the screen, there are many things to consider. Here are some code that distinguishes the screen from the bottom.


Method One: Use the event that triggers the switch between the phone's horizontal screen and the vertical screen

Window.addeventlistener ("Orientationchange", function () {    //announces the new direction of the value    alert (window.orientation);    

  


Method Two: Monitor the adjustment size change

/* Portrait *    /@media screen and (orientation:portrait) {/    * portrait-specific styles */    }/    * landscape *    /@media screen and (Orientation:landscape) {/    * landscape-specific styles */    }  

  

CSS Judging screen

/* Portrait *    /@media screen and (orientation:portrait) {/    * portrait-specific styles */}/*    Landscape * /    @media screen and (Orientation:landscape) {/    * landscape-specific styles */    }  

  

The local Window.matchmedia method allows real-time media queries. We can use the above media query to find out if we are in an upright or horizontal perspective:

var mql = Window.matchmedia ("(orientation:portrait)");//If there is a match, then we are in Vertical view if (mql.matches) {  //Upright Direction alert ("1")} else {  //Horizontal alert ("2")}  //Add a media query to change listener mql.addlistener (function (m) {if (m.matches) {// Change to upright direction document.getElementById ("test"). Innerhtml= "Change to Upright direction";} else {document.getElementById ("test"). Innerhtml= "Change to horizontal direction";//change to horizontal direction});

  


Js/css detect changes in the direction of mobile devices judging the screen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.