How to detect changes in the direction of mobile devices

Source: Internet
Author: User
Tags query window

Unless your application limits the use of mobile devices only in the upright or horizontal state, you will normally need to adjust some settings. Even if you design a sleek layout, you may need to change some programming code. There are usually several small strategies for detecting changes in the direction of a mobile device.

Orientationchange Events

You wait for a mobile API, a simple window Orientationchange event:

 
  
  
  1. Change in the direction of listening
  2. Window.addeventlistener ("Orientationchange", function () {
  3. The value of announcing the new direction
  4. alert (window.orientation);
  5. }, False);

The window.orientation attribute changes when the change occurs. A value of 0 indicates an upright, 90 means the device rotates horizontally to the left, and 90 indicates that the device is rotated horizontally to the right.

resizing events

Some devices do not support orientationchange events, but can trigger resize events:

 
  
   
  1. Monitor resizing changes
  2. Window.addeventlistener ("Resize", function () {
  3. Get screen size (internal/external width, internal/external height)
  4. }, False);

Compared with the Orientationchange incident is less obvious, but also very useful.

Screen size

There are some properties that can be recovered from the window object to get the screen size and I think the virtual screen size:

    • Outer width, outer height: pixels that are really fixed (rather than 320x356iphone vertical pixels)
    • Internal width, Internal height: virtual motionless pixels (instead of 320x356iphone upright pixels)

Of course, this data does not tell you the direction of the change, you need to pass the mathematical calculation, you know whether the current window state is wider or higher.

Media inquiries

You can also use CSS Media query to determine the direction:

 
  
    
  1. * Portrait * *
  2. @media screen and (orientation:portrait) {
  3. /* portrait-specific Styles * *
  4. }
  5. * Landscape * *
  6. @media screen and (Orientation:landscape) {
  7. /* landscape-specific Styles * *
  8. }

It's smarter to write a loop of "Watcher" in JavaScript to check the background color of a block and trigger your own change in direction.

Matching Media

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

 
  
     
  1. Find a match
  2. var mql = Window.matchmedia ("(orientation:portrait)");
  3. If there is a match, we are in the vertical view
  4. Upright direction
  5. Horizontal direction
  6. }
  7. Add a media query change supervisor
  8. Mql.addlistener (function (m) {
  9. if (m.matches) {
  10. Change to the upright direction
  11. }
  12. else {
  13. Change to a horizontal direction
  14. }
  15. });

These are some suggestions, I also hope to collect more people's methods!



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.