0

I'm having trouble with my image gallery. For some reason the image fades out but the rest of the code doesn't execute, it just stops mid way. I've tried removing the fade out function and the code works fine, the images are swapped how they should be etc. What is it about the fade that I'm doing wrong?

 $('.frame').click(function() {
    $("#lightbox").toggleClass("fadeout", function (){
    var copy = $(this).children().clone();
    $('#lightbox').children().replaceWith(copy);
    $("#lightbox").toggleClass("fadeout");
    });
});

I'm a complete self-taught newbie so I might ask some daft questions.

    #lightbox {
    opacity: 1;
    padding: 0 0 0 1%;
    width: 89%;
    height: 100%;
    float: right;
    text-align: center;
    transition: opacity 1s ease;
}

#lightbox.fadeout {
    opacity: 0;
}

.frame {
    width: 100%;
    text-align: left;
}

<div id="gallery_container">
<div id="lightbox">
    <img src="images/IMG_6412.jpg" alt="">
    </div>
<div id="gallery_nav">
    <ul>
        <div class="frame"><img src="images/XA2.jpg" alt=""></div>
        <div class="frame"><img src="images/Mega002.jpg" alt=""></div>
        <div class="frame"><img src="images/IMG_6412.jpg" alt=""</div>
    </ul>
</div>
    </div>

1 Answer 1

0

You're not using the toggleClass function properly. The second parameter is a function, but according to the documentation, the second parameter must be a state indication which is a boolean value (True = show, False = hide)

Based on your comments, here's something that should work:

$('.frame').click(function() {
    var copy = $(this).children().clone();
    $("#lightbox").fadeOut(2000, function (){
      $('#lightbox').children().replaceWith(copy);
      $("#lightbox").fadeIn(2000);
    });
});
9
  • Tried both of those and it still doesn't work, I tried fadeOut method and now it changes the image but it doesn't fade in and out. Your first suggestion faded the image but it didn't fade back in.
    – Steve Hird
    Commented May 30, 2020 at 1:46
  • Can you describe what your expected behavior is from beginning to end? That may help with understanding what isn't working.
    – Jocko
    Commented May 30, 2020 at 20:25
  • Also, have you played with the fadeOut time value (the 100 in the call) 100ms is really fast, you can slow it down to something awfully slow (like 5000) to confirm it is or is not working, then tune it to something you want.
    – Jocko
    Commented May 30, 2020 at 20:29
  • So essentially I want the image to fade out, then be replaced with another, then have the new image fade in. I'll try the fadeout timings and see if I can notice the change.
    – Steve Hird
    Commented May 31, 2020 at 21:27
  • I updated the answer with something that should work.
    – Jocko
    Commented Jun 1, 2020 at 20:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.