简体   繁体   English

获取隐藏图像的图像高度

[英]Get image height of hidden image

My page is divided into multiple tabs, corresponding to hidden divs. 我的页面分为多个选项卡,对应于隐藏的div。 One of the tabs has images, and I need to get the height of the images as the client sees it. 其中一个标签包含图片,我需要获取客户端看到的图片的高度。 I've been using image.clientHeight , $(image).height() , etc. but these all return 0 when the image is not visible on the page. 我一直在使用image.clientHeight$(image).height()等,但是当图像在页面上不可见时,这些都返回0。

My question is, is there any way to avoid this—ie get the height of image before the client sees it? 我的问题是,有什么办法可以避免这种情况-即在客户看到图像之前先获得图像的高度?

And to clarify, I'm looking for the height of the image as it appears on the client's page, not the original, full height of the image. 为了明确起见,我正在寻找图像在客户端页面上显示的高度,而不是图像的原始完整高度。

EDIT: So this is how my code is essentially laid out: 编辑:所以这就是我的代码的基本布局:

<div id="page1">
    <h1>Some landing page here</h1>
</div>

<div id="page2" style="display: none;">
    <h1>Images</h1>
    <img src="..."></img>
</div>

When the page loads, page1 is shown and all the others are hidden. 页面加载时,将显示page1,而其他所有页面均被隐藏。 I know all about checking when an image loads, the window loads, etc. but $(image).height() or all other variations of that don't work unless the image is visible to the client (ie page2 is shown). 我知道有关检查何时加载图像,加载窗口等的全部知识,但是$(image).height()或其他所有版本都无法正常工作,除非客户端可见该图像(即显示了page2)。 Is this intentional? 这是故意的吗? Is there any way around this? 有没有办法解决?

I think this happened because of image is not load. 我认为这是由于未加载图像而发生的。 So, try something like this. 所以,尝试这样的事情。

$('#myimg').on("load",function(){
    var imgHeight = img.get(0).height;
    var imgWidth = img.get(0).width;
})​

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

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