When using the jq width () method in ff and ie running problem is not big, but to chrome, there is a problem http://photo.163.com/shixiaojian089/train/28002 this is a Netease album, after seeing want to try to do it.
In my production method, I need to get the width of each photo, so I naturally use the jq width () method. It is not a problem to run in ff and ie, but the problem occurs in chrome.
Using alert for troubleshooting, we found that the value obtained by the width method in chrome is 0. In this case, the image is not loaded when the script runs here. The problem should be solved on $ (function () {});, because this method only requires the dom to be loaded to start running. So it can be executed under onload. However, this is obviously not a good method. After all, running in onload will wait until all the content of the entire file is loaded before running the script.
After searching online, I found this guy:
To obtain the image width, use
The Code is as follows:
$ Img. load (function (){
Var img_h = $ img. height ();
Var img_w = $ img. width ();
}
In this way, you can still use $ (function () {}); call the load method for the image object where the image is to be loaded to avoid waiting until the entire file content is loaded.