背景自动更改

时间:2015-02-04 17:30:23

标签: javascript jquery

我需要每隔3秒更改一次Jumbotron背景,但我的代码不起作用。怎么了?

var trocafundo = function() {
    var fundoimg = 1;
    var carrossel = function Troca() {
        if(fundoimg === 1) {
            $(".jumbotron").css("background-image","url('../images/bg2.png')");
            fundoimg = 2;
            } else {
            $(".jumbotron").css("backround-image","url('../images/bg1.png')");
            fundoimg = 1;
            }
        }
};

$(document).ready(
    setInterval(carrossel,1000);
);

1 个答案:

答案 0 :(得分:1)

这是关于变量范围的。 删除功能“trocafundo”。有关更多信息,请参阅此 http://www.w3schools.com/js/js_scope.asp

您还有一些语法错误,请参阅更正后的代码:

$(document).ready(function () {
    var fundoimg = 1;
    var carrossel = function () {
        if(fundoimg === 1) {
            $(".jumbotron").css("background-image","url('../images/bg2.png')");
            fundoimg = 2;
        } else {
            $(".jumbotron").css("background-image","url('../images/bg1.png')");
            fundoimg = 1;
        }
    }

    setInterval(carrossel,1000);
});

我认为这应该有用。