Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing jQuery to horizontally center images with different widths in a div
    primarykey
    data
    text
    <p>I have the following div:</p> <pre><code>&lt;div id="photos"&gt; &lt;div id="photos_cycle"&gt; &lt;img id="photo" src="{{ asset('bundles/tcheetenweb/images/photos/female_tennis_player.jpg') }}" /&gt; &lt;img id="photo" src="{{ asset('bundles/tcheetenweb/images/photos/foto_1.jpg') }}" /&gt; &lt;img id="photo" src="{{ asset('bundles/tcheetenweb/images/photos/foto_2.jpg') }}" /&gt; &lt;img id="photo" src="{{ asset('bundles/tcheetenweb/images/photos/foto_3.jpg') }}" /&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Which has an element photos_cycle with the different images, all with the same ID, here is where the problem comes in.</p> <p>I use the following javascript to cycle the images:</p> <pre><code>$('#photos_cycle').cycle({ fx: 'fade', timeout: 8000, speed: 0 }); </code></pre> <p>It cycles the image after 8 seconds, nothing exciting here.</p> <p>Now I want to center my images depending on their width I determine how the width of the photos_cycle should be so that I can center it:</p> <pre><code>setInterval(function () { $("#photos_cycle img").each(function() { imageWidth = this.clientWidth; if (imageWidth &gt; 0) { $("#photos_cycle").css('width', imageWidth); }); }, 10); </code></pre> <p>The only problem with this is that it works in Firefox and Chrome but not in IE... The biggest problem I find in this is that I'm having a problem identifying what image size I'm checking, because they all have the same id.</p> <p>I hope someone can help me with this.</p> <p>A live example of it can be found at <a href="http://test.tc.heeten.nl/" rel="nofollow">http://test.tc.heeten.nl/</a>, look at the block on the bottom right.</p> <p>Edit:</p> <p>I don't want to uniquely define the ID's because later I want to automatically add the images to the photos_cycle div, using some PHP code.</p> <p>This is the css I use for the divs:</p> <pre><code>#photos { background-color: #8ab7e2; width: 340px; height: 240px; position: relative; top: 60px; border-radius: 20px; -moz-border-radius: 20px; box-shadow: 0px 0px 1px 1px #000; } #photos_cycle { height: 240px; margin: 0 auto; position: relative; } #photos_cycle img { display: block; height: 200px; margin-top: 18px; position: position:absolute; left:50%; border: 2px solid #000; border-radius: 5px; -moz-border-radius: 5px; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload