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:
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?
You can use js setinterval to repeat the event of clicking the thumbnails.
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);
}
});
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.