Question

I'm trying to create an image banner that changes on click AND rotates automatically. I was able to use what user Oriol added to a previous post, "Image Gallery Thumbnails Slider," and it worked perfectly. This is the original jsfiddle:

http://jsfiddle.net/L7yKp/4/

var xml = "<rss version='2.0'><channel>"+ 
"<image>http://www.nikon.com/swf/img/thumbnail_ophthalmic_lenses.png</image>"+
"<image>http://www.nikon.com/swf/img/thumbnail_ophthalmic_lenses.png</image>"+
"<image>http://www.nikon.com/swf/img/thumbnail_ophthalmic_lenses.png</image>"+
"<limage>http://images1.videolan.org/images/largeVLC.png</limage>"+
"<limage>http://images1.videolan.org/images/largeVLC.png</limage>"+
"<limage>http://images1.videolan.org/images/largeVLC.png</limage>"+
"</channel></rss>",
$xml = $( $.parseXML(xml) ),
$images=$([
    //[elements, place, className],
    [$xml.find("image") ,"#thumbs",'thumbnails'],
    [$xml.find("limage"),"#panel" ,'largeimages']
]);
$images.each(function(){
    var that=this;
    that[0].each(function(){
        $(that[1]).append($('<img>', {class: that[2], src:$(this).text()}));
    });
});
$("#thumbs>img").click(function(){
    $("#thumbs>img.clicked").removeClass('clicked');
    $("#panel>img").hide();
    $("#panel>img:nth-child("+Number($(this).index()+1)+")").fadeIn();
    $(this).addClass('clicked');
});
$("#thumbs>img:first").click();
$('#next').click(function(){
    $('#thumbs>img.clicked').next().click();
});
$('#prev').click(function(){
    $('#thumbs>img.clicked').prev().click();
});

I was able to customize it so that my banner changes when I click a thumbnail or previous/next button. So, my question is...

What can I add to this code to make the banner change automatically as well? Is there something I can just insert here?

Was it helpful?

Solution

You can use js setinterval to repeat the event of clicking the thumbnails.

http://jsfiddle.net/L7yKp/99/

HTML

<input type="checkbox" name="autoplay" value="autoplay" />autoplay

JS

function autoPlay() {
    var nextImg = $('#thumbs>img.clicked').next();
    if (nextImg.length == 0) {
        nextImg = $('#thumbs>img').eq(0);
    }
    nextImg.click();
}
var autoInterval;
$('input[type="checkbox"][name="autoplay"]').click(function () {
    if ($(this).is(":checked")) {
        autoInterval = setInterval(autoPlay, 2000);
    } else {
        clearInterval(autoInterval);
    }
});

OTHER TIPS

Like this?

http://jsfiddle.net/samih/L7yKp/100/

Added a settimeout function which triggers the click event on the next element. you can see it at the bottom of the javascript section.

var runSlideShow = function(){
    setTimeout(slideShow, 3000);    
};

var slideShow = function(){
var images = $('#thumbs>img');
for (var i=0; i<images.length; i++) {
    if ($(images[i]).hasClass('clicked')){
        if (i === images.length - 1){
            $(images[0]).trigger('click');
        }
        else {
            $(images[i+1]).trigger('click');
        }
        break;
    }
}
    runSlideShow();
};

runSlideShow();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top