2017-01-27 3 views
0

w3schoolsの例の後にフレックススライダーを作成しようとしています。クリック時のフレックススライダーのリセットタイマー

クリックで自動的にスライドして新しいスライドがチャームのように機能しますが、ボタンをクリックするたびにタイマーをリセットする方法については頭がいっぱいです。

はい、私はjquery/javascriptに関する経験はほとんどありません。私はresetTimer()、t.reset()などのテストをしています。誰かがここで私を助けることができたらうれしいでしょう。 Sam0へ

おかげで、私はすでにコードを編集しました - どこかでまだ何か間違っている:

<script type="text/javascript"> 

var slideIndex = 0; 
carousel(); 

var repeater; 

function carousel() { 
var i; 
var x = document.getElementsByClassName("mySlides"); 
for (i = 0; i < x.length; i++) { 
    x[i].style.display = "none"; 
} 
slideIndex++; 
if (slideIndex > x.length) {slideIndex = 1} 
x[slideIndex-1].style.display = "block"; 
} 

function cycle(){ 
setInterval(function(){ 
    carousel(); 
}, 5000); 
} 

cycle(); 

function cycle(r){ 

if (r){ // if r is true then clear and restart the timer 
    clearInterval(repeater); // clear the timer 
    repeater = setInterval(function(){ // start the timer 
    carousel(); 
    }, 5000); 
} else { 
    clearInterval(repeater); // clear and stop the timer if r isn't true 
} 
} 

cycle(true); 


var slideIndex = 1; 
showDivs(slideIndex); 

function plusDivs(n) { 
showDivs(slideIndex += n); 
} 

function currentDiv(n) { 
cycle(true); 
showDivs(slideIndex = n); 
} 

</script> 
+0

あなたの前のスクリプトは現在不足していると呼ばれることができない 'showDivs'と呼ばれる機能を持っていました。このような方法で回答を修正しないことが賢明です。同様の問題を抱えている人が追跡しにくくなり、不連続性が生じるからです。代わりにコメントにフォローアップし、可能であればjsfiddleを使用して問題を説明してください。 – Sam0

答えて

1

あなたはcarousel()関数からsetTimeout(呼び出しを削除し、setInterval()と交換した場合、これはあなたが呼び出すことができるようになりますカルーセル機能を繰り返すタイマーに設定します。

var repeater; 

function cycle(r){ 

    if (r){ // if r is true then clear and restart the timer 
     clearInterval(repeater); // clear the timer 
     repeater = setInterval(function(){ // start the timer 
     carousel(); 
     }, 5000); 
    } else { 
     clearInterval(repeater); // clear and stop the timer if r isn't true 
    } 
} 

cycle(true); 

今:我々は、我々はその後、タイマーをリセットして再起動するハンドルとして使用することができます(私たちは機能の以前の外を定義する)プライベート変数にsetInterval()コールを割り当てることができます

function cycle(){ 
    setInterval(function(){ 
     carousel(); 
    }, 5000); 
} 

cycle(); 

任意の時点でcycle(false)に電話をかけてタイマーを停止させ、repeaterは一度に1つの値しか保持できないので、cycle(true)を呼び出すとタイマーをリセットして再起動する必要があります。あなたはタイマーをリセットしたい場合は、あなたのいずれかのボタンがクリックされるたびにそのようなあなたのcurrentDiv()関数内の任意の場所cycle(true)を追加します。

function currentDiv(n) { 
    cycle(true); 
    showDivs(slideIndex = n); 
} 

UPDATE

大スニペットの適応バージョンで以下のOPの編集されたコードとw3schoolsのコードで修正されました。

var i; 
 
    var slides = document.getElementsByClassName("mySlides"); 
 
    var dots = document.getElementsByClassName("dot"); 
 
    var slideIndex = -1; 
 
    var repeater; 
 

 
    function carousel() { 
 

 
    for (i = 0; i < slides.length; i++) { 
 
     slides[i].style.display = "none"; 
 
    } // cycle through and hide all the images 
 

 
    slideIndex++; 
 

 
    if (slideIndex > slides.length) { 
 
     slideIndex = 1; 
 
    } else if (slideIndex <= 0) { 
 
     slideIndex = slides.length; 
 
    } // what to do if slideIndex is too high or too low 
 

 
    slides[slideIndex - 1].style.display = "block"; 
 
    // show the relevant slide 
 
    } 
 

 
    function cycle(r) { 
 

 
    if (r) { // if r is true then clear and restart the timer 
 
     clearInterval(repeater); // clear the timer 
 
     repeater = setInterval(function() { // start the timer 
 
     carousel(); 
 
     }, 5000); 
 
    } else { 
 
     clearInterval(repeater); // clear and stop the timer if r isn't true 
 
    } 
 
    } 
 

 
    window.onload = function() { 
 
    carousel(); 
 
    }; // show the start image on load 
 

 
    carousel(); 
 
    cycle(true); 
 

 

 
    function plusDivs(n) { 
 
    cycle(true); 
 
    slideIndex += n - 1; 
 
    carousel(); 
 
    } 
 

 
    function currentDiv(n) { 
 
    cycle(true); 
 
    slideIndex = n - 1; 
 
    carousel(); 
 
    }
* { 
 
    box-sizing: border-box 
 
} 
 
body { 
 
    font-family: Verdana, sans-serif; 
 
    margin: 0 
 
} 
 
.mySlides { 
 
    display: none 
 
} 
 
/* Slideshow container */ 
 

 
.slideshow-container { 
 
    max-width: 1000px; 
 
    position: relative; 
 
    margin: auto; 
 
} 
 
/* Next & previous buttons */ 
 

 
.prev, 
 
.next { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: auto; 
 
    padding: 16px; 
 
    margin-top: -22px; 
 
    color: white; 
 
    font-weight: bold; 
 
    font-size: 18px; 
 
    transition: 0.6s ease; 
 
    border-radius: 0 3px 3px 0; 
 
} 
 
/* Position the "next button" to the right */ 
 

 
.next { 
 
    right: 0; 
 
    border-radius: 3px 0 0 3px; 
 
} 
 
/* On hover, add a black background color with a little bit see-through */ 
 

 
.prev:hover, 
 
.next:hover { 
 
    background-color: rgba(0, 0, 0, 0.8); 
 
} 
 
/* Caption text */ 
 

 
.text { 
 
    color: #f2f2f2; 
 
    font-size: 15px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    bottom: 8px; 
 
    width: 100%; 
 
    text-align: center; 
 
} 
 
/* Number text (1/3 etc) */ 
 

 
.numbertext { 
 
    color: #f2f2f2; 
 
    font-size: 12px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    top: 0; 
 
} 
 
/* The dots/bullets/indicators */ 
 

 
.dot { 
 
    cursor: pointer; 
 
    height: 13px; 
 
    width: 13px; 
 
    margin: 0 2px; 
 
    background-color: #bbb; 
 
    border-radius: 50%; 
 
    display: inline-block; 
 
    transition: background-color 0.6s ease; 
 
} 
 
.active, 
 
.dot:hover { 
 
    background-color: #717171; 
 
} 
 
/* Fading animation */ 
 

 
.fade { 
 
    -webkit-animation-name: fade; 
 
    -webkit-animation-duration: 1.5s; 
 
    animation-name: fade; 
 
    animation-duration: 1.5s; 
 
} 
 
@-webkit-keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 
@keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 
/* On smaller screens, decrease text size */ 
 

 
@media only screen and (max-width: 300px) { 
 
    .prev, 
 
    .next, 
 
    .text { 
 
    font-size: 11px 
 
    } 
 
}
<h2>Automatic Slideshow</h2> 
 
<p>Change image every 5 seconds:</p> 
 

 
<div class="slideshow-container"> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">1/3</div> 
 
    <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Text</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">2/3</div> 
 
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Two</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">3/3</div> 
 
    <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Three</div> 
 
    </div> 
 
    <a class="prev" onclick="plusDivs(-1)">❮</a> 
 
    <a class="next" onclick="plusDivs(1)">❯</a> 
 
</div> 
 
<br> 
 

 
<div style="text-align:center"> 
 
    <span class="dot" onclick="currentDiv(1)"></span> 
 
    <span class="dot" onclick="currentDiv(2)"></span> 
 
    <span class="dot" onclick="currentDiv(3)"></span> 
 
</div>

+0

ありがとうSam0、上記の私の編集OPをお読みください。私はあなたが提示したコードに関していくつか間違いを犯したと思う。前と同じように動作していますが、クリック時にはリセットされません。 – Franky2207

+0

ここには、 'showDivs()'関数が不要になった、更新されたコードのバージョンがあります。 https://jsfiddle.net/manoeuvres/q1s6h4jL/それがどのように動作するかを見るためにそれを再生します。 – Sam0

+0

ありがとうSam0!私を助けて、私の問題を解決しました。 – Franky2207

関連する問題