Not long ago, I did an H5 project and needed to do some processing when the horizontal and vertical screens changed. Of course, I needed to use orientationchange to monitor the changes of the horizontal and vertical screens. Solution 1:
After the code is added, various compatibility issues arise. The compatibility issues arise in two places:
The compatibility of orientationchange event is as follows: The following are the compatibility of screen.orientation: Option 2: The above solution doesn't work, so I have to find another way. After searching on Google, I found that it can be achieved by resizing (window.inner/outerWidth, window.inner/outerHeight):
This solution basically meets the needs of most projects, but it still has some shortcomings:
The key codes are as follows:
Click here for the complete code Option 3: However, I think that the detection by window.innerWidth > window.innerHeight is a pseudo detection, which is a bit unreliable. Can the detection be implemented through the browser? For example, based on CSS3@media media query. @media compatibility is as follows: As shown in the figure above, mobile browsers all support CSS3 media. Implementation ideas:
Here I choose the font-family of the <html></html> node as the detection style attribute. The reasons are as follows:
1. Give priority to the fonts that are listed first. 2. If the font cannot be found or does not include the text to be rendered, the next font is used. 3. If none of the listed fonts meet your needs, let the operating system decide which font to use. In this way, we can specify a specific logo to indicate the horizontal and vertical screen status, but the specified logo needs to be placed in front of other fonts so that it will not cause changes in the hmtl font. The key codes are as follows:
Click here for the complete code Test results
Option 4: It can be improved further. When orientationchange is supported, use the native orientationchange. If it is not supported, use solution three. The key codes are as follows:
Click here for the complete code Option 5: At present, the above solutions are all implemented through customized subscription and publishing event modes. Here, we can simulate orientationchange based on the browser's event mechanism, that is, fix the incompatibility of orientationchange. The key codes are as follows:
Click here for the complete code Through the above five solutions, I have a deeper understanding of mobile terminal horizontal and vertical screen detection. Some things can only be understood by yourself when you have experienced them. I also recorded the reasons in the article, hoping to help everyone. After the evolution of five solutions, the final orientationchange-fix was obtained, github address: https://github.com/zhansingsong/orientationchange-fix |
<<: What are Alibaba and JD.com’s VR+ shopping waiting for?
>>: iOS development history: from iPhone OS 1.0 to iOS10, finally supports harassment blocking
A few days ago, the People's Daily published ...
How to detect whether your WeChat friends have be...
In the turbulent Web 2.0 era, an important means ...
Recently, many netizens captured a wonderful scen...
If a basin of water is not placed properly, the w...
"The vision is beautiful, but making cars is...
If you are working in the education industry, you...
During APP operation , there are a large number o...
Author | Zhou Jihao Audit | Huang Jian Editor | Z...
Hydrogen energy innovation will set off a green r...
Actor Wang Xing, who was deceived and lost contac...
"Sitting with legs apart" is an indecen...
When it comes to potatoes, what is your first rea...
The metabolic engine we have evolved over million...
As a meat food that is "popular in the stree...