This article mainly introduces the basics of Mobile Web development - dealing with the horizontal and vertical screens of mobile devices. The window.orientation property, onorientationchange event and media query method are two solutions that need to be noted during the development process. Friends in need You can refer to the following
In order to cope with the fragmentation of mobile device screens, when we develop Mobile Web applications, one of the best practices is to adopt a fluid layout to ensure that the limited screen space is utilized to the maximum extent possible. Due to the directionality of the screen, after the user switches the direction of the screen, some design or implementation problems will become apparent - we at least need to deal with the adaptation of the width of the current display element (of course, we may have to do more than only this). Many times, we need to design corresponding application display modes for different screen orientations. At this time, it is extremely important to know the vertical screen status of the device in real time.
window.orientation attribute and onorientationchange event
window.orientation: This attribute gives the screen orientation of the current device, 0 means vertical screen, plus or minus 90 means horizontal Screen (left and right) mode
onorientationchange: This window event will be triggered every time the screen direction is switched between horizontal and vertical screens. The usage is similar to the traditional event
1: Use onorientationchange event The callback function to dynamically add an attribute called orient to the body tag, and at the same time define the corresponding style in css in the form of body[orient=landspace] or body[orient=portrait], so that it can be implemented on different screens Different styles are displayed in the mode. The following code example:
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;"> <title>横竖屏切换检测</title> <style type="text/css"> body[orient=landscape]{ background-color: #ff0000; } body[orient=portrait]{ background-color: #00ffff; } </style> </head> <body orient="landspace"> <p> 内容 </p> <script type="text/javascript"> (function(){ if(window.orient==0){ document.body.setAttribute("orient","portrait"); }else{ document.body.setAttribute("orient","landscape"); } })(); window.onorientationchange=function(){ var body=document.body; var viewport=document.getElementById("viewport"); if(body.getAttribute("orient")=="landscape"){ body.setAttribute("orient","portrait"); }else{ body.setAttribute("orient","landscape"); } }; </script> </body> </html>
2: Similar ideas are not implemented through CSS attribute selectors. The following code implementation plan:
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;"> <title>横竖屏切换检测</title> <style type="text/css"> .landscape body { background-color: #ff0000; } .portrait body { background-color: #00ffff; } </style> </head> <body orient="landspace"> <p> 内容 </p> <script type="text/javascript"> (function(){ var init=function(){ var updateOrientation=function(){ var orientation=window.orientation; switch(orientation){ case 90: case -90: orientation="landscape"; break; default: orientation="portrait"; break; } document.body.parentNode.setAttribute("class",orientation); }; window.addEventListener("orientationchange",updateOrientation,false); updateOrientation(); }; window.addEventListener("DOMContentLoaded",init,false); })(); </script> </body> </html>
Use media query method
This is a more convenient way, using pure CSS to achieve the corresponding function, the following code demonstration:
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;"> <title>横竖屏切换检测</title> <style type="text/css"> @media all and (orientation : landscape) { body { background-color: #ff0000; } } @media all and (orientation : portrait){ body { background-color: #00ff00; } } </style> </head> <body> <p> 内容 </p> </body> </html>
Smooth downgrade of lower version browsers
If the target mobile browser does not support media query and window.orientation does not exist, we need to use another way to achieve it———— Use a timer to compare the ratio of the height (window.innerHeight) and width (window.innerWidth) of the current window in "pseudo real-time" to determine the current horizontal and vertical screen status. As shown in the following code:
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;"> <title>按键</title> <style type="text/css"> .landscape body { background-color: #ff0000; } .portrait body { background-color: #00ffff; } </style> <script type="text/javascript"> (function(){ var updateOrientation=function(){ var orientation=(window.innerWidth > window.innerHeight)? "landscape" : "portrait"; document.body.parentNode.setAttribute("class",orientation); }; var init=function(){ updateOrientation(); window.setInterval(updateOrientation,5000); }; window.addEventListener("DOMContentLoaded",init,false); })(); </script> </head> <body> <p> 内容 </p> </body> </html>
Unified solution
By integrating the above two solutions, a cross-browser solution can be achieved, as follows Code:
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;"> <title>横竖屏切换检测</title> <style type="text/css"> .landscape body { background-color: #ff0000; } .portrait body { background-color: #00ffff; } </style> <script type="text/javascript"> (function(){ var supportOrientation=(typeof window.orientation == "number" && typeof window.onorientationchange == "object"); var updateOrientation=function(){ if(supportOrientation){ updateOrientation=function(){ var orientation=window.orientation; switch(orientation){ case 90: case -90: orientation="landscape"; break; default: orientation="portrait"; } document.body.parentNode.setAttribute("class",orientation); }; }else{ updateOrientation=function(){ var orientation=(window.innerWidth > window.innerHeight)? "landscape":"portrait"; document.body.parentNode.setAttribute("class",orientation); }; } updateOrientation(); }; var init=function(){ updateOrientation(); if(supportOrientation){ window.addEventListener("orientationchange",updateOrientation,false); }else{ window.setInterval(updateOrientation,5000); } }; window.addEventListener("DOMContentLoaded",init,false); })(); </script> </head> <body> <p> 内容 </p> </body> </html>
The above is the detailed content of Mobile terminal development: dealing with the horizontal and vertical screens of mobile devices. For more information, please follow other related articles on the PHP Chinese website!