http://photo.163.com/shixiaojian089/train/28002 This is a photo album from NetEase. I want to try it out after seeing it.
In my production method, I need to get the width of each photo, so I naturally used the width() method of jq. There is no big problem in running under ff and ie, but when it comes to Chrome, there is a problem.
Use alert to troubleshoot and found that the values obtained by the width method in Chrome are 0. In this case, I think the image is not loaded at all when the script is running here. The problem should be with $(function(){}); because this method only requires that the dom be run after loading. Then change it to execute under onload, and it's OK. However, this is obviously not a good solution. After all, you have to wait until all the contents of the entire file are loaded before running the script.
After searching online, I found that this guy //www.VeVB.COM/article/50402.htm also encountered the same problem. There is a solution below his comment, which can be used as a reference:
Use it where you want to get the width and height of the picture
The code copy is as follows:
$img.load(function(){
var img_h = $img.height();
var img_w = $img.width();
}
This way, you can still use $(function(){}); call the load method on the image object where the image needs to be loaded, avoiding waiting for the entire file content to load.