jquery背景图像旋转脚本不起作用

Cat*_*ish 0 javascript jquery

我正在编写这个脚本,每隔3秒旋转一个背景图像,但它根本不起作用,我很难过为什么不这样做.

$(document).ready(function() {

        var inc = 0;
        var bgImages = new Array();
        bgImages.push("Images/backgroundDog-small.jpg");
        bgImages.push("Images/backgroundDog1-small.jpg");
        bgImages.push("Images/backgroundDog2-small.jpg");
        bgImages.push("Images/backgroundDog3-small.jpg");

        setInterval(change, 3000);

        function change() {
            //if we're not at the end of the array
            if (inc < (bgImages.length)) {
                var image = bgImages[inc];
                $('body').css('backgroundImage', image);
                console.log(image);
                inc++;


            //reset the counter inc and go through the array again
            } else {
                inc = 0;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

Mat*_*chu 5

background-imageCSS中的属性不仅仅是图像URL; 你需要像样式表中那样写它:url("example.png")

$('body').css('backgroundImage', 'url("'+image+'")');
Run Code Online (Sandbox Code Playgroud)