获取按CSS百分比缩放的图像尺寸
我试图获取图像元素的尺寸(宽度),这些尺寸是通过 css 使用百分比值进行缩放的。
这是代码:
<div id="wrapper">
<ul id="gallery_content">
<li><img src="images/thumbs/1.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
<li><img src="images/thumbs/4.jpg"/></li>
<li><img src="images/thumbs/5.jpg"/></li>
<li><img src="images/thumbs/6.jpg"/></li>
<li><img src="images/thumbs/7.jpg"/></li>
<li><img src="images/thumbs/8.jpg"/></li>
<li><img src="images/thumbs/9.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
<li><img src="images/thumbs/4.jpg"/></li>
<li><img src="images/thumbs/5.jpg"/></li>
<li><img src="images/thumbs/6.jpg"/></li>
<li><img src="images/thumbs/7.jpg"/></li>
<li><img src="images/thumbs/8.jpg"/></li>
<li><img src="images/thumbs/9.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
</ul>
</div>
这里是 css:
ul#gallery_content li{ background:#FFF; list-style:none; display:inline; float:left; margin:0; height: 100%;}
ul#gallery_content li img { width:auto; height:100%; width:auto; }
我试图获取包装元素的实际宽度(所有图像宽度的总和)。 当在 a 中访问它们时,
$(document).ready(
galItems = $('#gallery_content').children().children();
galSize = 0;
$.each(galItems, function(index, item){
galSize += parseInt(item.width)
});
)
它会获取所有图像元素,但不会获取计算出的宽度。 有没有机会用jquery获取图像的实际渲染宽度?
I'm trying to get the dimensions (width) of image elements which are scaled using percentage values by css.
Here is the code:
<div id="wrapper">
<ul id="gallery_content">
<li><img src="images/thumbs/1.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
<li><img src="images/thumbs/4.jpg"/></li>
<li><img src="images/thumbs/5.jpg"/></li>
<li><img src="images/thumbs/6.jpg"/></li>
<li><img src="images/thumbs/7.jpg"/></li>
<li><img src="images/thumbs/8.jpg"/></li>
<li><img src="images/thumbs/9.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
<li><img src="images/thumbs/4.jpg"/></li>
<li><img src="images/thumbs/5.jpg"/></li>
<li><img src="images/thumbs/6.jpg"/></li>
<li><img src="images/thumbs/7.jpg"/></li>
<li><img src="images/thumbs/8.jpg"/></li>
<li><img src="images/thumbs/9.jpg"/></li>
<li><img src="images/thumbs/2.jpg"/></li>
<li><img src="images/thumbs/3.jpg"/></li>
</ul>
</div>
and here the css:
ul#gallery_content li{ background:#FFF; list-style:none; display:inline; float:left; margin:0; height: 100%;}
ul#gallery_content li img { width:auto; height:100%; width:auto; }
I'm trying to get the actual width of the wrapper-Element (total of all image-widths).
When accessing them in a
$(document).ready(
galItems = $('#gallery_content').children().children();
galSize = 0;
$.each(galItems, function(index, item){
galSize += parseInt(item.width)
});
)
It gets all the image elements but does not get the calculated width.
Is there any chance to get the actual rendered width of the images with jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过 .width() 作为函数?
http://api.jquery.com/width/
问题解决后编辑:
事实证明,问题实际上是 JavaScript 在页面和图像完全加载之前执行的。
通过将 $(document).ready() 更改为 $(window).load() 解决了这个问题。
Have you tried .width() as a function?
http://api.jquery.com/width/
Edit after the question was solved:
Turns out the problem actually was that the javascript was executed before the the page and the images was fully loaded.
This was solved by changing $(document).ready() to $(window).load().
在
ready()
事件中,您应该传递一个函数,因此将代码包装在函数中,如下所示:使用
+
符号而不是parseInt
> 将字符串转换为数字,速度更快。On the
ready()
event you should pass a function, so wrap you code within a function, like this:Use the
+
symbol instead ofparseInt
to convert strings to number, it's faster.