System-x32z
System-x32z

Reputation: 1971

jquery - slideshow buttons

I have problem that I'm trying to figure out a solution for it from a long time.
Here's my slideshow: http://jsfiddle.net/Jtec5/33/
Codes: CSS:

#slideshow {
    margin: 50px auto;
    position: relative;
    width: 240px;
    height: 240px;
    padding: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
#slideshow > div {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
}

#slideshow img {
    max-width:240px;
    max-height:240px;
}

ul {
    list-style:none;
    margin:0px;
    padding:0px;
}
ul li {
    float:left;
    border-radius:10px;
    width:10px;
    height:10px;
    border:1px solid white;
    background:grey;
}
ul li.active {
    background:black;
}

HTML:

<div id="slideshow">
    <div>
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg" />
    </div>
    <div>
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg" />
    </div>
    <div>
        <img src="http://gillespaquette.ca/images/stack-icon.png" />
    </div>
</div>
<ul></ul>

Jquery:

$("#slideshow > div:gt(0)").hide();

var maxindex = $('#slideshow > div').length;

var index = 0
var interval = 3 * 1000; // 3 seconds
var timerJob = setInterval(traverseSlideShow, interval);

function traverseSlideShow() {
    console.log("current index: " + index);

    $('#slideshow > div')
        .stop()
        .fadeOut(1000);
    $('#slideshow > div').eq(index)
        .stop()
        .fadeIn(1000);

    $('ul li').removeClass('active');
    $('ul li:eq(' + index + ')').addClass('active');
    index = (index < maxindex - 1) ? index + 1 : 0;

}

for (var i = 0; i < maxindex; i++) {
    $('ul').append('<li class="' + (i == 0 ? 'active' : '') + '"></li>');
}

$(document).on('click', 'ul li', function () {
    index = $(this).index();
    traverseSlideShow();
    clearInterval(timerJob);
    timerJob = setInterval(traverseSlideShow, interval);
});

I have circle buttons <ul> in the slideshow that takes you to the photo it's related with, the problem begins if you click on this button much times, you can see the picture pause for very short time and continue to appear, every extra click pauses the photo for short time from appearing.

Upvotes: 0

Views: 553

Answers (1)

Sergio
Sergio

Reputation: 28845

I added a if statement to check if the this index in the click is the same as the image being showed. And in that case return false.

if (last_index == $(this).index()) {
    return false
}

Demo here

Upvotes: 1

Related Questions