2017-04-17 178 views
-4

您好我已經建立了一個圖像滑塊與導航箭頭使用HTML和JavaScript。我需要一個暫停按鈕來停止幻燈片放映,因爲它每5秒旋轉一次圖像。誰能幫我暫停javascript圖像滑塊

<script> 
 

 
\t var index = 1; 
 

 
\t function plusIndex(n){ 
 
\t \t index = index + 1; 
 
\t \t showImage(index); 
 
\t } 
 

 
\t showImage(1); 
 

 
\t function showImage(n){ 
 
\t \t var i; 
 
\t \t var x = document.getElementsByClassName("slides"); 
 
\t \t if(n > x.length){ index = 1}; 
 
\t \t if(n < 1){ index = x.length}; 
 
\t \t for(i=0;i<x.length;i++) 
 
\t \t \t { 
 
\t \t \t \t x[i].style.display = "none"; 
 
\t \t \t } 
 
\t \t x[index-1].style.display = "block"; 
 
\t } 
 
\t autoSlide(); 
 
\t function autoSlide(){ 
 
\t \t var i; 
 
\t \t var x = document.getElementsByClassName("slides"); 
 
\t \t for(i=0;i<x.length;i++) 
 
\t \t \t { 
 
\t \t \t \t x[i].style.display = "none"; 
 
\t \t \t } 
 
\t \t if(index > x.length){ index = 1} 
 
\t \t x[index-1].style.display = "block"; 
 
\t \t index++; 
 
\t \t setTimeout(autoSlide,5000); 
 
\t } 
 

 
</script>
<div id="container_slides"> 
 
\t <img class="slides"src="IMG EXAMPLE"/> 
 
\t <img class="slides"src="IMG EXAMPLE"/> 
 
\t <img class="slides"src="IMG EXAMPLE"/> 
 
\t <img class="slides"src="IMG EXAMPLE"/> 
 
\t <img class="slides"src="IMG EXAMPLE"/> 
 

 
\t <button class="btn" onclick="plusIndex(-1)" id="btn1">&#10094;</button> 
 
\t <button class="btn" onclick="plusIndex(1)"id="btn2">&#10095;</button> 
 
\t 
 
</div>

呢?

+1

問題尋求幫助調試(「**爲什麼不是這個代碼工作?**」)必須包括所期望的行爲,一*特定問題或錯誤*和*最短的代碼必要*在問題本身**中重現它**。沒有**明確問題陳述**的問題對其他讀者沒有用處。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+1

閱讀[如何問](http://stackoverflow.com/help/how-to-ask)。我們需要示例代碼,告訴我們您做了什麼以及您遇到問題的位置。 – Oen44

+0

你可以檢查示例代碼,它不會運行。如果可以的話,請糾正它。 –

回答

0

您需要爲此使用clearTimeout()。所以,你指定計時器到一個全局變量(因爲它應該是可訪問的交叉功能),然後:

var a = 0; // Global 

a = setTimeout(autoSlide, 5000); // Replace this line. 

,並創建一個功能:

function stop() { 
    clearTimeout(a); 
} 

將它添加到一個按鈕:

<button class="btn" onclick="stop();">Stop</button> 

全部摘錄

var index = 1; 
 
var a = 0; 
 

 
function plusIndex(n) { 
 
    index = index + 1; 
 
    showImage(index); 
 
} 
 

 
showImage(1); 
 

 
function showImage(n) { 
 
    var i; 
 
    var x = document.getElementsByClassName("slides"); 
 
    if (n > x.length) { 
 
    index = 1 
 
    }; 
 
    if (n < 1) { 
 
    index = x.length 
 
    }; 
 
    for (i = 0; i < x.length; i++) { 
 
    x[i].style.display = "none"; 
 
    } 
 
    x[index - 1].style.display = "block"; 
 
} 
 
autoSlide(); 
 

 
function autoSlide() { 
 
    var i; 
 
    var x = document.getElementsByClassName("slides"); 
 
    for (i = 0; i < x.length; i++) { 
 
    x[i].style.display = "none"; 
 
    } 
 
    if (index > x.length) { 
 
    index = 1 
 
    } 
 
    x[index - 1].style.display = "block"; 
 
    index++; 
 
    a = setTimeout(autoSlide, 5000); 
 
} 
 

 
function stop() { 
 
    clearTimeout(a); 
 
}
<div id="container_slides"> 
 
    <img class="slides" src="//placehold.it/100?text=1" /> 
 
    <img class="slides" src="//placehold.it/100?text=2" /> 
 
    <img class="slides" src="//placehold.it/100?text=3" /> 
 
    <img class="slides" src="//placehold.it/100?text=4" /> 
 
    <img class="slides" src="//placehold.it/100?text=5" /> 
 

 
    <button class="btn" onclick="plusIndex(-1)" id="btn1">&#10094;</button> 
 
    <button class="btn" onclick="plusIndex(1)" id="btn2">&#10095;</button> 
 
    <button class="btn" onclick="stop();">Stop</button> 
 
</div>

演示:http://output.jsbin.com/guyanifogu

1

現在,您可以暫停,並用單一的點擊取消暫停。用於檢查滑塊是否處於暫停狀態的附加變量,如果它設置爲true,則防止執行setTimeout。並且爲了確保該圖像在暫停時不會改變,在功能showImageautoSlide開頭處的好聲明if應該有所幫助。

var index = 1; 
 
var timeout = null; 
 
var paused = false; 
 

 
function plusIndex(n) { 
 
    index++; 
 
    showImage(index); 
 
} 
 

 
function stopSlider(event) { 
 
    paused = !paused; 
 
    event.innerHTML = paused ? "Start" : "Stop"; 
 
    paused ? clearTimeout(timeout) : timeout = setTimeout(autoSlide, 5000); 
 
} 
 

 
showImage(1); 
 

 
function showImage(n) { 
 
    if (paused) return; 
 
    var i; 
 
    var x = document.getElementsByClassName("slides"); 
 
    if (n > x.length) { 
 
    index = 1 
 
    }; 
 
    if (n < 1) { 
 
    index = x.length 
 
    }; 
 
    for (i = 0; i < x.length; i++) { 
 
    x[i].style.display = "none"; 
 
    } 
 
    x[index - 1].style.display = "block"; 
 
} 
 
autoSlide(); 
 

 
function autoSlide() { 
 
    if (paused) return; 
 
    showImage(index); 
 
    index++; 
 
    timeout = setTimeout(autoSlide, 5000); 
 
}
<div id="container_slides"> 
 
    <img class="slides" src="IMG EXAMPLE" alt="IMG 1" /> 
 
    <img class="slides" src="IMG EXAMPLE" alt="IMG 2" /> 
 
    <img class="slides" src="IMG EXAMPLE" alt="IMG 3" /> 
 
    <img class="slides" src="IMG EXAMPLE" alt="IMG 4" /> 
 
    <img class="slides" src="IMG EXAMPLE" alt="IMG 5" /> 
 

 
    <button class="btn" onclick="plusIndex(-1)" id="btn1">&#10094;</button> 
 
    <button class="btn" onclick="stopSlider(event.target)" id="btn3">Stop</button> 
 
    <button class="btn" onclick="plusIndex(1)" id="btn2">&#10095;</button> 
 

 
</div>

+1

你沒有解釋任何東西......':('但是一個很好的方式來暫停和取消暫停! –

+1

作出編輯,我應該提出一些解釋之前 – Oen44

+0

現在看起來不錯。 –