Solve the problem of jQuery dynamically obtaining the height and width of the mobile screen

  • 2020-03-30 02:50:07
  • OfStack

Today, I helped my colleagues to solve the problem of using jQuery to get dynamic phone screen width and height.

The problem started when a colleague used an image control that required full screen scrolling and needed to get the height of the screen to set the image to full screen. However, he could not reach the height() for the first time with $(window).height(), which was ok after refreshing. I tried to use window.onload=function(){} to solve the problem, but still failed.

Finally, I think it must be because the second refresh, the browser has the cache, so it can get high, so it must be the time difference of page loading DOM, which causes this problem, so setTimeout(method name,500) is used; After a delay of 500 milliseconds, the result is normal.

So when your friends can't get the width and height of the screen, try this

Related articles: