Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create a jQuery image rotator but can only get the first half of it to work
    primarykey
    data
    text
    <p>I'm trying to create an image rotator where one image will fade into the next and so on until it loops back to the first image. I got the first two images to work correctly but the remaining 3 don't fade correctly. They just jump into view.</p> <p>I know I still need to account for the reaching the end of the rotation. I haven't taken care of that yet because I'm just trying to get the first round of rotation to work.</p> <p>What am I missing to get this to work correctly?</p> <p>My HTML:</p> <pre><code> &lt;div id="imageRotator"&gt; &lt;div id="image01" class="current"&gt; &lt;img src="images/imageRotate01.jpg"&gt; &lt;/div&gt; &lt;div id="image02" class="next"&gt; &lt;img src="images/imageRotate02.jpg"&gt; &lt;/div&gt; &lt;div id="image03" class="hidden"&gt; &lt;img src="images/imageRotate03.jpg"&gt; &lt;/div&gt; &lt;div id="image04" class="hidden"&gt; &lt;img src="images/imageRotate04.jpg"&gt; &lt;/div&gt; &lt;div id="image05" class="hidden"&gt; &lt;img src="images/imageRotate05.jpg"&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>My CSS:</p> <pre><code>#imageRotator div { position: absolute; } .current { z-index: 1; } .next { z-index: 0; } .hidden { z-index: -1; } </code></pre> <p>My jQuery:</p> <pre><code>$(document).ready(function() { var firstImage = $('#image01'); var currentImage, nextImage; setInterval(rotateImages, 2000); function rotateImages() { currentImage = $('div.current'); nextImage = $('div.next'); //the first image fades away currentImage.animate({opacity: 0}, 2000, function() { currentImage.removeClass('current'); }); //bring the second image into view nextImage.animate({opacity: 1}, 2000, function() { //make the new image the current image nextImage.removeClass('next').addClass('current'); //the next image in the rotation becomes the 'next' image nextImage.next().addClass('next').removeClass('hidden'); }); } }); </code></pre>
    singulars
    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.
    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