0

I'm looking for a really simple and lightweight code that change the body background of the website (it can go with the css also) every 10 seconds, it's supposed to be something easy with jquery and css, right?

how can i do that?

VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
user2250871
  • 21
  • 2
  • 2

3 Answers3

0

You can use JavaScript's setTimeout() function. Here is an example of how to use it.

For setTimeout(), you need 2 parameters, one being your function, and the other an integer, which is how long your timeout will last.

So for your instance, you can do:

$(function() {
    var newBg = ['img1.png', 'img2.jpg', 'img3.png'];
    var path="img/";
    var i = 0;

    var changeBg = setTimeout(function() {
        $('body').css({backgroundImage : 'url(' path + newBg[i] + ')'});
        if(i == newBg.length)
            i=0;
        else
            i++;
    }, 10000);
});

This creates an array of your images, specified with a path. Thus I create a variable which checks the length of your arrayList, and whether or not you need to reset the variable i.

So i hope that helps :)

madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38
  • it's not working for me... how the markup / html should look like? – user2250871 Apr 06 '13 at 16:53
  • `code` Test Page

    Hello World!

    `code`
    – madcrazydrumma Apr 06 '13 at 17:00
0

custom-background.js is a lightweight jQuery plugin to allow users change the website’s default background and saves the background selected to local storage. This plugin can easily be added to any website or web app without any compromise on website's performance.

https://github.com/CybrSys/custom-background.js

Shubham Badal
  • 1,125
  • 13
  • 33
0

You can easily do this with jQuery.

A simple javascript function to toggle between two colors every X seconds function toggle_color(color1,color2,cycle_time,wait_time) {

setInterval(function first_color() {
    $("body").animate({
        backgroundColor: color1
    }, 1000, function () {
        setTimeout(change_color, wait_time);
    });
}, cycle_time);

function change_color() {
    $("body").animate({
        backgroundColor: color2
    }, 1000, function () {
        setTimeout(function () {}, wait_time);
    });
}
}

You can check Changing background color every X seconds in Javascript for working demo and explanation.

Narendran Parivallal
  • 1,070
  • 1
  • 14
  • 16