基本上我試圖用jquery創建幻燈片。這裏的幻燈片導航工作正常,但幻燈片循環無法正常工作。
我在這裏想我jsfiddle
我的腳本是:如何使用jQuery循環播放幻燈片
var currIndex = -1;
function slideshow(slideshowId){
var len=$("ul#"+slideshowId+" li.slides").length;
currIndex = (currIndex + 1) % len;
$("ul#"+slideshowId+" li.slides").hide();
$("ul#"+slideshowId+" li.slide"+currIndex+"").fadeIn();
}
function slidepagenation(slideshowId,slideno){
$("ul#"+slideshowId+" li.slides").hide();
$("ul#"+slideshowId+" li.slide"+slideno+"").fadeIn();
currIndex = slideno;
}
var tshone = 0;
var tshtwo = 0;
slideshow('slideshow-one');
tshone =setInterval(slideshow('slideshow-one'), 500);
slideshow('slideshow-two');
tshtwo =setInterval(slideshow('slideshow-two'), 500);
$('ul.slideshow-one li a').click(function(){
clearInterval(tshone);
var element = $(this);
var slideshowid=element.parent().parent().attr('class');
var slideno = $("ul.slideshow-one li a").index($(this));
slidepagenation(slideshowid,slideno);
});
$('ul.slideshow-two li a').click(function(){
clearInterval(tshtwo);
var element = $(this);
var slideshowid=element.parent().parent().attr('class');
var slideno = $("ul.slideshow-two li a").index($(this));
slidepagenation(slideshowid,slideno);
});
我的HTML是在這裏:
<ul class="slideshow" id="slideshow-one">
<li class="slides slide0" style="display:block">
Slide one
</li>
<li class="slides slide1">
Slide two
</li>
<li class="slides slide2">
Slide three
</li>
</ul>
<div class="full-length clearfix txt-center">
<ul class="slideshow-one">
<li><a>1</a></li>
<li><a>2</a></li>
<li><a>3</a></li>
</ul>
</div>
<br><br>
<ul class="slideshow" id="slideshow-two">
<li class="slides slide0" style="display:block">
Slide one
</li>
<li class="slides slide1">
Slide two
</li>
</ul>
<div class="full-length clearfix txt-center">
<ul class="slideshow-two">
<li><a>1</a></li>
<li><a>2</a></li>
</ul>
</div>
你試圖使用一個'currIndex'可變保住兩艘幻燈片的軌道。 (加上TJ下面說的。)另外,爲什麼在這兩個函數中重複'.hide()'和'.fadeIn()'?從'slideshow()'中調用'slidepagenation(slideShowId,(currIndex + 1)%len)'。 – nnnnnn
爲什麼不使用50個已創建的幻燈片插件之一? – Jared