在幻灯片中使用StopInterval()

时间:2014-12-08 01:34:25

标签: javascript javascript-events slideshow

我有一个幻灯片,每隔5秒移动到下一张幻灯片。它还允许用户单击按钮跳转到特定幻灯片并继续幻灯片放映。但是,当单击按钮时,它会在幻灯片上花费10秒钟而不是5秒。有关如何允许用户更改幻灯片的任何提示,让它在该幻灯片上花费5秒钟,然后转到下一个页面即可。

使用Javascript:

(function() {

    var buttons = document.querySelectorAll("#pagenation li");
    var slides = document.querySelector("#slider ul");
    var individSlides = document.querySelectorAll("#slider ul li");
    var i = 1;
    var slideHeight = document.querySelector(".slide").clientHeight;
    var myTimer = setInterval(moveSlide, 4800);


    for (var q=0; q<buttons.length; q++) {
      (function(q) {
        buttons[q].addEventListener('click', function(){
            clearInterval(myTimer);
            slides.style.top = (0 - (slideHeight*q)) + "px";
            $(buttons).removeClass("selected");
                $(buttons[q]).addClass("selected");
                i = q;
                if (i == 2){
                    i++;
                }
                myTimer = setInterval(moveSlide, 4800);
        })
      }(q));
    }

    //Move to the next slide and repeat if at the end
    function moveSlide() {
        if(i >= individSlides.length) {
            i = 0;
        }
        slides.style.top = (0 - (slideHeight*i)) + "px";
        $(buttons).removeClass("selected");
        $(buttons[i]).addClass("selected");
        i++;
    } //end MoveSlide()

})(); //end function()

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/styles.css" type="text/css">
<script src="js/jquery-2.1.1.js"></script>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1>Andy's Aquatics</h1>
        </header>
        <nav>
            <ul id="mainNav">
                <li>My Tank</li>
                <li>Product Reviews</li>
                <li>Photo Gallery</li>
            </ul>
        </nav>
        <div id="slideWrapper">
            <div id="slider">
                <ul>
                    <li>
                        <img class="slide" src="images/bg.png" height="200" width="650" usemap="#slide1">
                        <map name="slide1">
                            <area shape="rect" coords="0,0,650,200" href="test.html">
                        </map>
                    </li>
                    <li>
                        <img class="slide" src="images/bg2.png" height="200" width="650"usemap="#slide2">
                        <map name="slide2">
                            <area shape="rect" coords="0,0,650,200" href="test1.html">
                        </map>
                    </li>
                    <li>
                        <img class="slide" src="images/bg3.png" height="200" width="650" usemap="#slide3">
                        <map name="slide3">
                            <area shape="rect" coords="0,0,650,200" href="test2.html">
                        </map>
                    </li>
                </ul>
            </div>
            <ul id="pagenation">
                <li class ="selected">.</li>
                <li>.</li>
                <li>.</li>
            </ul>
        </div>
    </div>

    <script src="js/script.js"></script>

</body>
</html>

CSS:

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

body {
    position: relative;
}

#mainNav li{
    display: inline-block;
    padding: 5px;
}

            /****** Slider *****/
#slideWrapper {
    width: 650px;
    height: 200px;
    display: block;
    margin: 0px auto;
}
#slider {
    float: left;
    position: relative;
    z-index: 1;
    overflow: hidden;
    width: 650px;
    height: 200px;
}

#slider ul {
    width: 650px;
    position: absolute;
    top: 0px;
}

#slider li {
    list-style: none;
    float: left;
    width: 650px;
    height: 200px;
}

#pagenation {
    position: relative;
    bottom: 80px;
    color: #FFF;
    left: 4px;
    list-style: none;
    font-size: 70px;
    font-weight: 700;
    z-index: 2;
    height: 0;
}

#pagenation li{
    cursor: pointer;
    float: left;
    padding: 6px;
    margin-right: 5px;
    text-align: center;
}

#pagenation li:hover {
    color: #666;
}

.selected {
    color: #666
}

1 个答案:

答案 0 :(得分:0)

问题在于你的逻辑。让我们说当前幻灯片是第一张幻灯片(i = 0)。用户单击第二张幻灯片(q = 1)。

首先发生的是清除间隔,选择幻灯片,设置i = 1并设置计时器。

然后5秒钟后,对moveSlide的调用执行,它将所选幻灯片设置为i(仍为1)。并将i增加到2。

经过另外5秒,现在i = 2,因此选择了最后一张幻灯片。

有几种方法可以解决这个问题,但我认为现在您已经知道了可以自行解决的问题,您的代码会显示您知道自己的内容。