Hi guys i am trying to modify this code here.
$(document).ready(function() {
var randomImages = ['img-1','img-2','img-3','img-4'];
var rndNum = Math.floor(Math.random() * randomImages.length);
$("div#bg-image").css({ background: "url(http://example.com/images/" + randomImages[rnd开发者_JAVA技巧Num] + ".jpg) no-repeat" });
});
This works fine and gives me a random image each time which is great but what i need is for the image to increment +1 each time on page refresh.
This is because sometimes i can refresh 3 times and will still get the same image show when using Math.random(). I need to cancel the random and set +1 each time.
Any help
You need to store this number as a cookie. Then every time you refresh the page you know the previous number so you can either escape it or increment it by 1.
For reading/writing cookies use some of the plugins available, e. g. http://plugins.jquery.com/project/cookie
精彩评论