交通灯jquery插件

时间:2013-10-13 15:15:00

标签: javascript jquery

我写了jQuery插件,模拟交通信号灯的工作。您可以看到演示here和源代码:

$.fn.plugin = function (b/*time*/,a/*starting with given color*/) {
    b.push(b[1]);//adding phases time to plugin array 
    return this.each(function (f, d) {
        var
            e = ["#008000", "#FFFF00", "#FF0000", "#FFFF00"],//green,yellow,red,yellow
            c = function () {
                $(d).html(b[a])// here i want to place time, that left for current phase, it should change dynamicly
                    .css({
                        "background-color": e[a]//changing bg as light
                    });
             window.setTimeout(c, 1000 * b[a]);//hold phase as long as we told to plugin
             a = ++a % 4//result would be 0..1..2..3 and so on
            };
        c()
    })
};

$(window).load(function(){
$("#c1").plugin([2,2,2],0);
$("#c2").plugin([2,2,2],2);
})

</head>
<body>
Starts with red:<span id="c1" style="">&nbsp;</span><br>
Starts with green:<span id="c2" style="">&nbsp;</span><br>

所以,你可以看到我正在选择元素,添加插件,编写绿色,黄色,红色的相位时间,以及应该从什么颜色开始交通灯。

$(d).html(b[a])

这段代码只是为当前阶段写出时间。 我希望这个数字显示该阶段的剩余时间。

0 个答案:

没有答案