我正在编写此脚本以每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;
            }
        }
    });

最佳答案

CSS中的background-image属性不仅仅包含图像URL。您需要像在样式表中一样实际编写它:url("example.png")

$('body').css('backgroundImage', 'url("'+image+'")');

09-07 13:04