5

重複の可能性:
Javascriptで単純なスライドショーメソッドを作成しようとしています

JavaScript を使用してカルーセル画像のスライドショー スクリプトを作成する方法を学びました。フレームワーク(インスタントスクリプト)から何かを作るよりも、基本から学ぶ方が良いと思いますが、私は初心者です。この台本は僕のテクニックで書いていますが、ひどいと思います。

OK、これが私の質問です。このスライドショーをループさせる方法がわかりません。誰か助けてください。ありがとう

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        #wrapper {
        width:400px;
        height:140px;
        position:absolute;
        left:50%;
        top:50%;
        margin: -70px 0 0 -200px;
        background: #383838;
        overflow: hidden;
        }

        #abc-container {
        position: absolute;
        width:1200px;
        height:140px;
        }

        #a {
        width:400px;
        height:140px;
        background: #FF0000;
        float: left;
        }

        #b {
        width:400px;
        height:140px;
        background: #FFFF00;
        float: left;
        }

        #c {
        width:400px;
        height:140px;
        background: #00FFFF;
        float: left;    
        }
    </style>
</head>
<body>
    <div id="wrapper">
        <div id="abc-container">
            <div id="a"></div>
            <div id="b"></div>
            <div id="c"></div>
        </div>
    </div>
    <div id="asas"></div>
    <div id="asass"></div>
    <script type="text/javascript">
        var runCarousel, runTimer;
        firstval = 0;
        secondval = 0;

        function Carousel(){
        firstval += 10;
        document.getElementById('abc-container').style.left = "-" + firstval + "px";
        document.getElementById('asas').innerHTML = "-" + firstval;
            if(firstval == 400)
            {
                StopRun();
                StartTimer()
                return;
            }
            if(firstval == 800)
            {
                StopRun();
                StartTimer()
                return;
            }
        runCarousel = setTimeout(Carousel, 10);
        }

        function StartTimer(){
        secondval += 1;
        document.getElementById('asass').innerHTML = "-" + secondval;
        if(secondval == 10)
        {
            StopTimer();
            Carousel();
            return;
        }
        if(secondval == 20)
        {
            StopTimer();
            Carousel();
            return;
        }
        runTimer = setTimeout(StartTimer, 1000);
        }

        function StopRun(){
        window.clearTimeout(runCarousel);
        }

        function StopTimer(){
        window.clearTimeout(runTimer);
        }

        function firstStart(){
            setTimeout("Carousel()", 10000);
        }

        firstStart();
    </script>
</body>
</html>
4

1 に答える 1

3

最初にエラーがあります:

function firstStart(){
     setTimeout("Carousel()", 10000);
}

正しく:

function firstStart(){
     setTimeout(Carousel, 10000);
}

最後に、すべての設定がデフォルトにリセットされ、タイマーが開始されます。

カルーセル:

if(firstval == 1200){
        document.getElementById('abc-container').style.left = "0" + "px";
        firstval = 0;
        StopRun();
        StartTimer()
        return;
    }

StartTimer で

if(secondval == 30) {
        secondval = 0;
        StopTimer();
        Carousel();
        return;
    }

デモ

これが私の例です

于 2012-10-01T09:43:50.417 に答える