[英]Apply children image width and height to container
I have this markup: 我有这个标记:
<div class="img_container">
<img src="file://localhost/img/a_bg.jpeg" alt="a_bg" />
</div>
How can I apply the width and height of the image to the img_container? 如何将图像的宽度和高度应用于img_container?
The image is on position absolute 图像在绝对位置
Untested, but I'd suggest: 未经测试,但我建议:
$('.img_container img').each(function(){
var w = this.naturalWidth,
h = this.naturalHeight;
$(this).parent().css({'height': h, 'width' : w});
});
Markup: 标记:
You will need to add an id
to the image as below: 您将需要向图像添加一个
id
,如下所示:
<div class="img_container">
<img id="bgimg" src="file://localhost/img/a_bg.jpeg" alt="a_bg" />
</div>
Jquery: jQuery的:
var img = $('#bgimg');
img.load(function(){
//Get width of image
var width = img.width();
//Get height of image
var height = img.height();
$('.img_container').css("width", width);
$('.img_container').css("height", height);
});
Try this 尝试这个
<img src="file://localhost/img/a_bg.jpeg" alt="a_bg width="X" height="Y" />
And add this to you CSS 并将其添加到您的CSS
background-image: url(file://localhost/img/a_bg.jpeg);
background-size: Xpx Ypx;
background-repeat:no-repeat;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.