background switch page load
I've seen a lot of different background switching based upon link classes, however I just want a s开发者_C百科imple background switcher on any page load (no cookies) and not link dependent.
is there a simple method for this? jquery?
Are looking for javascript timers?
Example:
setInterval("switchBackground()", 1000);
function switchBackground( )
{
$("body").css("backgroundImage", "xxx");
}
I hope this help!
Edit:
To generate random background at start:
$(document).ready(function() {
//This will be random from bg1.jpg to bg10.jpg
$("body").css("backgroundImage", "/images/bg"+GenerateNumber(10)+".jpg");
//1 to max
function GenerateNumber(max) {
return Math.floor(Math.random()*max) + 1;
}
});
The code for a random background image provided by Mouhannad didn't work for me, I had to make a few small edits in order for it to function:
$(document).ready(function() {
//This will be random from bg1.jpg to bg10.jpg
$("body").css("background-image", "url(/images/bg"+GenerateNumber(10)+".jpg)");
//1 to max
function GenerateNumber(max) {
return Math.floor(Math.random()*max) + 1;
}
});
I needed to change the "backgroundImage" declaration to "background-image" and also added "url()" to the image path. JavaScript uses the CamelCase notation for CSS properties, jQuery does not.
精彩评论