How to change the website’s background image dynamically?

Asked

Viewed 1,193 times

0

So I wanted to make a bg-image if it changes dynamically the same in this example: http://www.goesenlinea.com/agus/

Background changes every 5 seconds.

Does anyone know how ?

1 answer

2


You can do it via Javascript with a timer running 5 in 5 sec. See example:

// initial call, or just call refresh directly
setInterval(AsyncTrocaImg, 5000);


function AsyncTrocaImg() {
    $.post(base + "Home/GetNewImg", {}, function (URL) {
         $('body').css('background-image', 'url('+URL+')');
    }).fail(function () {
        console.log("error");
    })
}

But be careful with the images, so your site doesn’t get "heavy".

I would also have some other forms even simple, but this is the way I would do.

Also see if these links help you:

CSS several backgrounds

Dynamically change a class background

Browser other questions tagged

You are not signed in. Login or sign up in order to post.