自动更改背景 [英] Background Changing Automatically

查看:45
本文介绍了自动更改背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每3秒更改一次Jumbotron背景,但是我的代码无法正常工作.怎么了?

I Need to change my Jumbotron background every 3 seconds, but my code didn't work. What's wrong?

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

推荐答案

这与变量范围有关. 删除功能"trocafundo".请参阅此以获取更多信息: http://www.w3schools.com/js/js_scope.asp

It's about the variable scope. Remove the function "trocafundo". See this for more information: http://www.w3schools.com/js/js_scope.asp

您还存在一些语法错误,请参见更正的代码:

You also have some syntax errors, see corrected code:

$(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);
});

我认为这应该可行.

这篇关于自动更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆