简体   繁体   English

Javascript Image OnLoad事件

[英]Javascript Image OnLoad event

I know I can attach an event listener to a loading image so that it'll trigger when the image finishes loading like this: 我知道我可以将一个事件监听器附加到加载图像,以便在图像完成加载时触发它:

<img id="mine" src="image.jpg" />

<script>
$('#mine').load(function() {
  console.log($(this).height()); //The image's height
  console.log($(this).width()); //The image's width
})
</script>

But my browser knows how big the image is going to be as soon as the header is loaded - can I tap into that event, so that as soon as the browser knows the image's size I can resize elements on my page? 但是我的浏览器知道加载标题后图像的大小 - 我可以点击该事件,这样一旦浏览器知道图像的大小,我就可以调整页面上的元素大小吗?

Thanks! 谢谢!

No, you can't. 不,你不能。 But, you should pass the image's dimensions along with the tag. 但是,您应该将图像的尺寸与标签一起传递。

<img id="mine" src="image.jpg" width="100" height="100">

For example, in PHP you can use the getimagesize() function. 例如,在PHP中,您可以使用getimagesize()函数。

It's even a recommended practice to improve browser rendering. 甚至推荐的做法是改进浏览器渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM