2011-08-11 37 views
0

我有以下代碼,我已經實現了這樣的東西,但我想添加「NEXT」,「PREV」功能,爲此我需要suggesstion。簡單的JQuery滑塊,不想使用插件

<div id="slider"> 
<div class="wrap"> 
<div class="panel">Panel1</div> 
<div class="panel">Panel2</div> 
<div class="panel">Panel3</div> 
</div> 

<div class="previous">This need Implementation</div> 
<div class="next">This need Implementation</div> 

</div> 

<div class="nav"> 

<ul> 
<li> panel1</li> 
<li> panel2</li> 
<li> panel3</li> 
<ul> 

</div> 


$(document).ready(function() { 

$(".wrap .panel:not(.active)").fadeOut(); 
$(".wrap .panel:first(.active)").fadeIn(); 

$(".nav ul li").click(function (event) { 

$(this).addClass('current').siblings().removeClass('current'); 

$(".wrap .panel").stop(true, true).fadeOut().eq($(this).index()).fadeIn(); 

}); 
}); 

回答

0

你需要存儲當前窗格的指數顯示

var currentIndex = 0;// store current pane index displayed 
var ePanes = $('#slider .panel');// store panes collection 
function showPane(index){// generic showPane 
    // hide current pane 
    ePanes.eq(currentIndex).stop(true, true).fadeOut(); 
    // set current index : check in panes collection length 
    currentIndex = index; 
    if(currentIndex < 0) currentIndex = ePanes.length-1; 
    else if(currentIndex >= ePanes.length) currentIndex = 0; 
    // display pane 
    ePanes.eq(currentIndex).stop(true, true).fadeIn(); 
    // menu selection 
    $('.nav li').removeClass('current').eq(currentIndex).addClass('current'); 
} 
// bind ul links 
$('.nav li').click(function(ev){ showPane($(this).index());}); 
// bind previous & next links 
$('.previous').click(function(){ showPane(currentIndex-1);}); 
$('.next').click(function(){ showPane(currentIndex+1);}); 
// apply start pane 
showPane(0); 
+0

大dmidz,快點,它這是工作。只有一個問題,當你點擊下一個和第三個幻燈片後,它隱藏了一切,我怎麼能去第一個,在第三個之後。同樣適用於prev。 – JAML

+0

而不是: 'currentIndex = Math.max(0,Math.min(ePanes.length-1,index));' 寫下如下內容: 'if(currentIndex <0)currentIndex = ePanes.length-1' 'else if(currentIndex> = ePanes.length)currentIndex = 0' – dmidz

+0

對不起,你可以編輯上面的代碼。我不知道,在哪裏。 – JAML