Random background from Colourlovers
This page uses the API from the COLOURlovers website to set a random background based on the top patterns. This is done by taking the following simple steps:
- Use JSONP call to retrieve a JSON list of the 50 top patterns.
- Select one of the elements at random.
- Use the css "background-image" property to set the background.
All you have to do this is (for more info see this article on http://www.smartjava.org):
// We retrieve a set of the 50 top backgrounds
$.getJSON('http://www.colourlovers.com/api/patterns/top?jsonCallback=?&numResults=50',
function(data) {
// we randomly select one of the elements
var element = data[Math.floor(Math.random()*50)];
// we update the body background with the url
$('body').css({
"background-image": "url("+ element.imageUrl +")"
});
});