Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues recalling an animation function whilst making a jQuery image gallery. Next/Previous buttons
    primarykey
    data
    text
    <p>this is my first ever post to Stack Overflow and it's because I've been tearing my hair out over this issue for the past day.</p> <p>I'm pretty new to jQuery and have a basic understanding of how things work but I'm really struggling with getting a function to loop back on itself after it has processed. It's a simple if/else function that runs through a series of five images to see if they are visible or not. </p> <p>If an image is visible (in other words, the current image in a gallery) then the next and previous buttons will control which image is next to be faded in and subsequently fade out the previous image.</p> <p>I have the Next/Previous links working for the first image but I can't get it to work once the second image has faded in. I'm guessing it's because I need to recall the script for it to check if/else the images are visible. My problem is that I don't know where to place the code. I've tried the code in various places and I've been researching into jQuery delays and callbacks to see if I can try and work out whether it's the timings not working etc. But I just can't work it out!</p> <p>If anyone can help get this working or even suggest a simpler way of creating this that I don't know, I would be greatly appreciative!</p> <p>The images are simply listed within a div:</p> <pre><code>&lt;div class="images"&gt; &lt;img class="image gallery-img" id="1" src="/images/01.jpg" /&gt; &lt;img class="image gallery-img" id="2" src="/images/02.jpg" /&gt; &lt;img class="image gallery-img" id="3" src="/images/03.jpg" /&gt; &lt;img class="image gallery-img" id="4" src="/images/04.jpg" /&gt; &lt;img class="image gallery-img" id="5" src="/images/05.jpg" /&gt; &lt;/div&gt; &lt;div class="gallery-controls"&gt; &lt;a class="gallery-button" id="previous" href="#previous"&gt;Previous&lt;/a&gt; &lt;a class="gallery-button" id="next" href="#next"&gt;Next&lt;/a&gt; &lt;/div&gt; </code></pre> <p>And the jQuery is:</p> <pre><code>window.addEventListener( 'DOMContentLoaded', function(){ function galleryLoop(){ if( $('div.images img:nth-child(1)').css({ 'display' : 'inline' }) ){ //#NEXT CLICK $('a.gallery-button#next').bind('click', function(){ $('div.images img:nth-child(1)').fadeOut( '100' , function(){ $('div.images img:nth-child(2)').fadeIn(); });//END FADE OUT }); //END #NEXT CLICK //#PREVIOUS CLICK $('a.gallery-button#previous').bind('click', function(){ $('div.images img:nth-child(1)').fadeOut( '100' , function(){ $('div.images img:nth-child(5)').fadeIn(); });//END FADE OUT }); //END #PREVIOUS CLICK } //END IF else if( $('div.images img:nth-child(2)').css({ 'display' : 'inline' }) ){ //#NEXT CLICK $('a.gallery-button#next').bind('click', function(){ $('div.images img:nth-child(2)').fadeOut( '100' , function(){ $('div.images img:nth-child(3)').fadeIn(); });//END FADE OUT }); //END #NEXT CLICK //#PREVIOUS CLICK $('a.gallery-button#previous').bind('click', function(){ $('div.images img:nth-child(2)').fadeOut( '100' , function(){ $('div.images img:first-child').fadeIn(); });//END FADE OUT }); //END #PREVIOUS CLICK }; //END IF }; galleryLoop(); </code></pre> <p>});//END SCRIPT</p>
    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