1

My slideshow works, however when the last image is reached it stays stuck there and won't continue, when I refresh the page it starts at the first image again and works untill the last image is reached.

HTML:

       <div class="slider">

        <img src="slide1_hamburger.jpg" class="slide_image slider_active"></img>
        <img src="slide2_eetzaal.jpg" class="slide_image"></img>
        <img src="slide3_carpaccio.jpg" class="slide_image"></img>

    </div>

CSS:

.slide_image {
    display: block;
    margin-left: auto;
    margin-right: auto;
    left: 0;
    right: 0;
    position: absolute;
    opacity: 0;
      transition: opacity 2s ease-in-out;
   -moz-transition: opacity 2s ease-in-out;
   -webkit-transition: opacity 2s ease-in-out;
}


.slider_active {
    opacity: 1;
    transition: opacity 2s ease-in-out;
   -moz-transition: opacity 2s ease-in-out;
   -webkit-transition: opacity 2s ease-in-out;
}

Jquery:

function slideSwitch() {
        var $slider_active = $('div.slider IMG.slider_active');
        var $next = $slider_active.next();    

        $next.addClass('slider_active');

        $active.removeClass('slider_active');
    }

    $(function() {
        setInterval( "slideSwitch()", 5000 );
});

Does anyone have an idea why it stops at the last image and won't continue? Thanks in advance.

3 Answers 3

2

It stops at the last image because the routine tries to get the .next() element, which is not there.

You need to add a check to see if $next actually exists and then go back to the start if it doesn't. Something like:

function slideSwitch() {
    var $slider_active = $('div.slider IMG.slider_active');
    var $next = $slider_active.next();    

    if(!$next) {
        $next = $('div.slider IMG')[0];
    }

    $next.addClass('slider_active');

    $slider_active.removeClass('slider_active');
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});
3
  • I try to implant it but the $next = $('div.slider IMG')[0]; make it not go back to the start Commented Jul 9, 2015 at 13:07
  • A made a correction to the last line in slideSwitch() function, it was using an incorrect variable name Commented Jul 9, 2015 at 13:12
  • Sadly after the last image it just fades to a white screen :( Commented Jul 9, 2015 at 13:14
1

You unused $active object in the code. If you remove the class .slider_active and one more correction your setInterval only works for mozila if you want all browsers supports

use like

setInterval(slideSwitch, 5000 );

just use

$('.slider_active').removeClass('slider_active');

your code be

function slideSwitch() {
        var $slider_active = $('div.slider IMG.slider_active');
        var $next = $slider_active.next();  
        $('.slider_active').removeClass('slider_active');
        $next.addClass('slider_active');
        if($next.length == 0){  // for cyclic rotation
         $('div.slider IMG:eq(0)').addClass('slider_active');
        }
 }
$(function() {
    setInterval(slideSwitch, 5000);
});

Fiddle sample

0
1

All you have to do is point the $next variable to the first image once it has gotten to the end of the images.

replace var $next = $slider_active.next(); with

var $next =  $slider_active.next().length ? $slider_active.next() : $('div.slider IMG:first');
3
  • This make the whole thing not work anymore :(, I understand that their is something wrong with the $next variable but this solution sadly does not work Commented Jul 9, 2015 at 13:07
  • Ah, I forgot a .. Updated my awnser $slider_activenext() should have been $slider_active.next()
    – Jo Oko
    Commented Jul 9, 2015 at 13:08
  • Thanks for the update, now it works but it still stops after the last image :/ Commented Jul 9, 2015 at 13:12

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