Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:43+00:00 2026-06-05T12:07:43+00:00

this is my first ever post to Stack Overflow and it’s because I’ve been

  • 0

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.

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.

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.

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!

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!

The images are simply listed within a div:

<div class="images">
    <img class="image gallery-img" id="1" src="/images/01.jpg" />
    <img class="image gallery-img" id="2" src="/images/02.jpg" />
    <img class="image gallery-img" id="3" src="/images/03.jpg" />
    <img class="image gallery-img" id="4" src="/images/04.jpg" />
    <img class="image gallery-img" id="5" src="/images/05.jpg" />
</div>

<div class="gallery-controls">
    <a class="gallery-button" id="previous" href="#previous">Previous</a>
    <a class="gallery-button" id="next" href="#next">Next</a>
</div>

And the jQuery is:

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();  

});//END SCRIPT

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:07:44+00:00Added an answer on June 5, 2026 at 12:07 pm

    Ok I spent a lot of time on this so I hope it solves your problem!

    Here is the link to the JSFiddle: http://jsfiddle.net/ekUvj/22/

    What you want to do is change you javascript to this:

    var imgNum = 1;
    $("#1").fadeIn(300);
    
    
    function prev () {
        newImageNum=(imgNum == 1)?5:imgNum-1;
        $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
        imgNum = newImageNum;
    }
    
    function next () {
        newImageNum=(imgNum == 5)?1:imgNum+1;
        $("#".concat(imgNum)).fadeOut(300, function () {$("#".concat(newImageNum)).fadeIn(300);});
        imgNum = newImageNum;
    }​
    

    And then make sure your buttons call the functions:

    <div class="gallery-controls">
        <button onclick="prev()">Previous</a>
        <button onclick="next()">Next</a>
    </div>​
    

    Then make sure your sure your images are hidden at first in the CSS:

    .gallery-img {
        display: none;
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface I've rarely ever been a JS developer and this is my first attempt
This is probably the worst question I have ever posted on Stack Overflow but
Original Question: This is my first post ever. I'm trying to learn ios programming
i'm new to this kind of things. Kinda the first post ever regarding programming,
this is my first ever post here. I'm not sure if I've done the
first post ever here I'm trying to replicate this sort of JSON object so
This is my first ever post on any forum, so excuse my etiquette. Im
This is my first post ever :). I have quite a bit of experience
First post ever so please forgive if I am out of line. StreamWriter noteswriter
Hello everyone I am a newbie to php and this is my first ever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.