开发者

jQuery - set div width to sum of it's children

开发者 https://www.devze.com 2022-12-21 10:39 出处:网络
I have following setup: html: <div id=\"holder\"> <div> <img/> <img/> </开发者_JAVA技巧div>

I have following setup:

html:

<div id="holder">
  <div>
    <img/>
    <img/>
  </开发者_JAVA技巧div>
</div>

css

#holder { width: 800px; }

now, i want to make jQuery gallery and will move line of images to the left and right with negative margin-left of inner div.

However, I have to tell <img/>s to float to left and set inner div's width to sum of widths of images.

How can I do this some clean and nice way?

thank you


var sum=0;
$('#holder img').each( function(){ sum += $(this).width(); });
$('#holder > div').width( sum );

//alert(sum);

this should do the trick ..

The float to the images, you can apply it with css

#holder img{float:left;}
0

精彩评论

暂无评论...
验证码 换一张
取 消