[英]Make image banner rotate automatically AND on click
我正在嘗試創建一個圖像橫幅,該橫幅在單擊時會發生變化並自動旋轉。 我能夠使用用戶Oriol在上一篇文章“ Image Gallery Thumbnails Slider”中添加的內容,並且效果很好。 這是原始的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();
});
我能夠對其進行自定義,以便在單擊縮略圖或上一個/下一個按鈕時更改橫幅。 所以,我的問題是...
我可以在此代碼中添加什么以使橫幅也自動更改? 我可以在這里插入什么嗎?
您可以使用js setinterval重復單擊縮略圖的事件。
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);
}
});
像這樣?
http://jsfiddle.net/samih/L7yKp/100/
添加了settimeout函數,該函數將觸發下一個元素的click事件。 您可以在javascript部分的底部看到它。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.