简体   繁体   中英

Changing background-image in CSS with JQuery

I am creating a simple slider who is just fading a picture over an other. The problem is that the background change only works in the dreamweaver preview but not in the browser.

loop = setInterval(function(){
    $("#slider").css("background-image" , "url(/slider" + x + ".jpg)");
    $("#slider > img").fadeOut(2);
    $("#slider > img#" + y).fadeIn(3000);
}, 4000)

So it basicly looks like this: Set the background, fade the old pic away, fade the new pic over the background, do it again. But I can't see why it won't set the background or won't display it at least.

You need to change the src attribute of the image like this: $("#slider > img").attr('src', y); . Please check this jsfiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM