Detection of mobile device screen

Source: Internet
Author: User

    • Usage Scenarios

Mobile side of the development process, it is unavoidable to judge the screen, and then perform other operations, such as loading different styles, horizontal screen display some content, vertical screen display other content and so on.

    • How to judge

The mobile device provides two objects, one property, and one event:

    • Window.orientation belongs to a property on the Window object, there are three values: 0 is portrait mode (Portrait), 90 is left inverted to landscape mode (landscape), 90 is turned right to landscape mode (landscape), The last 180 is the device up or down swap or vertical screen mode.
    • Orientationchange is an event that fires when the device rotates, as if the Resize event was used on the PC.

These two are used together, it is easy to get the screen status of the device, the code is as follows:

(function(){    varinit =function(){        varUpdateorientation =function(){            varOrientation =window.orientation; Switch(orientation) { Case90:                 Case-90: Orientation= ' Landscape ';  Break; default: Orientation= ' Portrait ';  Break; }           //Do something           //like adding a state to an HTML tag            //then, depending on the state, the different sizes are displayedDocument.body.parentNode.setAttribute (' class ', orientation);        }; Window.addeventlistener (' Orientationchange ', updateorientation,false);    Updateorientation ();    }; Window.addeventlistener (' domcontentloaded ', init,false);}) ();

 /*  * vertical div Border shows blue *  */ .portrait body div  { border : 1px solid blue ;} /*  * vertical div Border shows yellow *  */ .landscape body div 
{ border : 1px solid yellow ;} 

Of course you can also use media queries (this is recommended):

{  body div {    Border:1px solid blue;  }  {  body div {    Border:1px solid yellow;  } }
    • Compatibility

What happens if window.orientation or orientationchange is not present in the device? (The general one does not exist, the other does not exist, and vice versa)

In the early development, Chrome's device model is often used, but this property does not exist, how to get this value? The simple way is based on the width and height of the comparison judgment, the width is less than the height of the vertical screen, wide and high is the horizontal screen.

Get the results of the way to know, the next is how to monitor the device reversal event, since Orientationchange is not available, use the most basic resize event or use timer detection, or look at the code:

(function(){    varUpdateorientation =function(){        varOrientation = (Window.innerwidth > Window.innerheight)? ' Landscape ': ' Portrait '; Document.body.parentNode.setAttribute (' Class ', orientation);    }; varinit =function() {updateorientation (); //check once every 5 seconds        //Window.setinterval (updateorientation,5000);                //Monitoring Resize EventsWindow.addeventlistener (' Resize ', updateorientation,false);    }; Window.addeventlistener (' domcontentloaded ', init,false);}) ();

Finally, combining the above two methods is a complete detection solution

(function(){    varSupportorientation = (typeofWindow.orientation = = = ' Number ' &&typeofWindow.onorientationchange = = = ' object '); varinit =function(){        varHtmlnode =Document.body.parentNode, orientation; varUpdateorientation =function(){            if(supportorientation) {Orientation=window.orientation; Switch(orientation) { Case90:                     Case-90: Orientation= ' Landscape ';  Break; default: Orientation= ' Portrait ';  Break; }            }Else{Orientation= (Window.innerwidth > Window.innerheight)? ' Landscape ': ' Portrait '; } htmlnode.setattribute (' Class ', orientation);        }; if(supportorientation) {Window.addeventlistener (' Orientationchange ', updateorientation,false); }Else{            //Monitoring Resize EventsWindow.addeventlistener (' Resize ', updateorientation,false);    } updateorientation ();    }; Window.addeventlistener (' domcontentloaded ', init,false);}) ();

Reference:

Dealing with Device Orientation

Detection of mobile device screen

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.