使用 JQuery 添加完整的背景图像

Add full background image using JQuery

本文关键字:背景 图像 JQuery 添加 使用      更新时间:2023-09-26

我正在构建小脚本,一个带有描述的全屏幻灯片,但我在添加图像时遇到了一些问题,似乎无法识别图像。

http://jsfiddle.net/w7c3gzvd/

我也想添加此代码

"no-repeat center center fixed;"

以修复背景。

我做错了什么?

var i = 1;
function slideshow() {
    setTimeout(function() {
        if (i == 2) {
            $("body").css("http://static.vecteezy.com/system/resources/previews/000/046/003/original/fuchsia-background-with-light-beams-no-bar.jpg)");
            $("#description").text("teste1");
        }
        if (i == 3) {
            $("body").css("http://i1.wp.com/static.web-backgrounds.net/uploads/2012/08/City_Landscape_Background.jpg)");
            $("#description").text("teste2");
        }
        if (i == 4) {
            $("body").css("http://4.bp.blogspot.com/-AISNX1W0-ww/UXZ80eprU8I/AAAAAAAAAoI/e4-7I79LmKI/s1600/background+wallpaper+6742.jpg)");
            $("#description").text("teste3");
            i = 1;
        }
        i++;
        if (i < 5) {
            slideshow();
        }
    }, 1000)
}
slideshow();

谢谢

你没有正确实现 jquery 的 .css()。 您必须将属性名称和值作为参数传递给它 http://api.jquery.com/css/

.css( propertyName, value )

因此,要设置背景图像(以及所需的其他参数),您可以使用:

$(body).css('background','url("http://static.vecteezy.com/system/resources/previews/000/046/003/original/fuchsia-background-with-light-beams-no-bar.jpg") no-repeat center center fixed');

你只是将css设置为图像网址。您需要将 css 背景图像 url 设置为图像的 url。

$("body").css("background-image", "url('your photo url here')")