我有一個簡單的幻燈片式應用程序,它是在我的網頁的多個部分。每個幻燈片放映都有一個數組,用於保存每個幻燈片的特定信息。我遇到的問題是,當我使用一個幻燈片並循環訪問數組中的最後一個元素,然後轉到另一個幻燈片演示時,我需要多次單擊才能讓新陣列開始循環播放。我知道問題是,我創建的計數器變量沒有重置,但我不知道爲什麼或如何解決它。誰能提供解決方案?幻燈片計數器不進行重置多個陣列
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();
}
}
可以把VAR計數器= 0;放入SlideShow函數的第一行? –
@Jonasw SlideShow函數或SlideShowControls函數? – Robert
好吧,我想我應該更好地建議你重寫你的整個代碼,看到我的答案。 –