2017-02-15 65 views
0

我有一個簡單的幻燈片式應用程序,它是在我的網頁的多個部分。每個幻燈片放映都有一個數組,用於保存每個幻燈片的特定信息。我遇到的問題是,當我使用一個幻燈片並循環訪問數組中的最後一個元素,然後轉到另一個幻燈片演示時,我需要多次單擊才能讓新陣列開始循環播放。我知道問題是,我創建的計數器變量沒有重置,但我不知道爲什麼或如何解決它。誰能提供解決方案?幻燈片計數器不進行重置多個陣列

HTML

<section id="games" class="page"> 
    <div class="slideshow"> 
     <div class="screen"></div> 
     <div class="desc"> 
      <p>Solar System Sim is a 3D simulation of our galaxy built in Unity and written in C#. Clicking on each planet in the simulation will zoom and center on the planet, while clicking on the sun will return the camera to its original position.</p> 
     </div> 
     <div class="controls"> 
      <span class="control left"></span> 
      <span class="control right"></span> 
     </div> 
    </div> 
</section> 
<section id="webApps" class="page"> 
    <div class="slideshow"> 
     <div class="screen"></div> 
     <div class="desc"> 
      <p>Word Count</p> 
     </div> 
     <div class="controls"> 
      <span class="control left"></span> 
      <span class="control right"></span> 
     </div> 
    </div> 
</section> 

JS

var counter = 0; 
var screen = document.getElementsByClassName("screen"); 
var control = document.getElementsByClassName("control"); 
for(var i = 0; i < control.length; i++){ 
    control[i].onclick = function(){ 
     var leftArrowClicked = this.classList.contains("left"); 
     var rightArrowClicked = this.classList.contains("right"); 
     SlideShowControls = function(){ 
      if(leftArrowClicked){ 
       counter--; 
      } 
      if(rightArrowClicked){ 
       counter++; 
      } 
      SlideShow = function(slides, whichSlideShow){ 
       var desc = document.getElementsByClassName("desc")[whichSlideShow].getElementsByTagName("p")[0]; 
       if(counter < 0){ 
        counter = slides.length - 1; 
       } 
       if(counter > slides.length - 1){ 
        counter = 0; 
       } 
       desc.textContent = slides[counter][0]; 
       screen[whichSlideShow].style.backgroundImage = "url(" +slides[counter][1] +")"; 
      } 
      if(CurrentPage.page == "games"){ 
       SlideShow(games, 0); 
      } 
      if(CurrentPage.page == "webApps"){ 
       SlideShow(webApps, 1); 
      } 
      console.log(counter); 
     } 
     SlideShowControls(); 
    } 
} 
+0

可以把VAR計數器= 0;放入SlideShow函數的第一行? –

+0

@Jonasw SlideShow函數或SlideShowControls函數? – Robert

+0

好吧,我想我應該更好地建議你重寫你的整個代碼,看到我的答案。 –

回答

2

您可能要綁定您的幻燈片顯示在每個方向對,使他們不互相影響:

function SlideShow(slides,descid, left, right){ 
//to prevent bugs: 
if(!(slides&&descid!==undefined&&left&&right)){console.error("missing args");return;} 

//the magic unique counter 
var counter=0; 

//the event handlers: 
left.onclick=function(){counter--;update()}; 
right.onclick=function(){counter++;update();}; 

//the function to redraw the slide 
function update(){ 
var desc = document.getElementsByClassName("desc")[descid].getElementsByTagName("p")[0]; 
    if(counter < 0){ 
    counter = slides.length - 1; 
    } 
    if(counter > slides.length - 1){ 
    counter = 0; 
    } 
    desc.textContent = slides[counter][0]; 
    screen[descid].style.backgroundImage = "url(" +slides[counter][1] +")"; 
} 

//draw the slide the first time 
update(); 
} 

如下使用:

var arrow=document.getElementsByClassName("control"); 
SlideShow(games,0,arrow[0],arrow[1]); 
SlideShow(webApps,1,arrow[2],arrow[3]); 

主要問題是,只有一次存在計數器,而您有多張幻燈片。我已將計數器放入slideShow函數,因此它通過閉包綁定到事件偵聽器。香港專業教育學院還調整了您的代碼,它的更換你的

+1

默認值在函數中尚未標準化! –

+1

@HajjiTarik yep。實施錯誤投擲,而不是... –

+0

真棒,謝謝你,它的作品完全是我想要的方式。我試圖用閉包(或者我認爲我理解他們)的方式來做到這一點,但並不確定如何去做,而且確實是做錯了。 – Robert

0

在這個代碼:

if(leftArrowClicked){ 
    counter--; 
} 
if(rightArrowClicked){ 
    counter++; 
} 

,當你達到0或N,是NE的你應該考慮到幻燈片的數量,否則,你可以得到負或飽和值,並點擊箭頭的時候什麼都不會moveuntil你達到一個有效值

+0

謝謝,但這並沒有真正回答這個問題。 – Robert

+0

可以看看正確的代碼背後... –

+0

我剛剛測試在的jsfiddle和它的作品完美https://jsfiddle.net/whnzwzuq/ – Carles

0

1 - 我建議你要避免出現任何問題,你必須封裝你的代碼中的函數,把它稱爲。

2 - 檢查這個

  if(leftArrowClicked && counter != 0){ 
       counter--; 
      } 
      if(rightArrowClicked && counter <= slides.length){ 
       counter++; 
      } 

我希望這有助於你

+1

1.是的,但要麼得到更具體的,要麼是相當的評論。 2.用戶已經實現了不同的行爲(無限幻燈片) –