在没有jQuery的情况下制作循环JavaScript幻灯片

时间:2012-10-01 09:16:04

标签: javascript slideshow

  

可能重复:
  trying to create simple Slide show method in Javascript

我已经学会用JavaScript制作轮播图片幻灯片脚本。我认为从基础学习它比从框架(即时脚本)制作一些东西更好,但我是新手。我用我的技术写这个剧本,但我认为这很糟糕。

好的,这是我的问题: 我不知道如何制作这个幻灯片循环,任何人都可以帮助我? 感谢

<!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>

1 个答案:

答案 0 :(得分:3)

首先出现错误:

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

正确:

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

最后,所有设置都重置为默认值并启动计时器:

在Carousel中:

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;
    }

DEMO

Here is my example