Saturday, March 26, 2011

Mfarhanonline:Change Background Image on Refresh with Javascript

Fav Tag:

Mfarhanonline Dev & Designer: Aol.com home page has changing the background image on every refresh, quite interesting. So how to apply this kind of effect to your web project, take a quick look this post. It's simple just five lines of javascript code. Use it and enrich your web project user interface.

Background Image Change on Refresh with Javascript

Download Script 

The tutorial contains a folder called bgimages with background images files.

index.html
bgimages
1.jpg
2.jpg
3.jpg
– ….
– ….

Javascript
Here variable totalCount is number of backgrounds. The Math.random() method returns a random number between 0 and 1 and the Math.ceil() method rounds a number towards to the nearest integer, and returns the result.

<html>
<head>
<script type="text/javascript">
var totalCount = 8;
function ChangeIt()
{
var num = Math.ceil( Math.random() * totalCount );
document.body.background = ‘bgimages/’+num+’.jpg’;
document.body.style.backgroundRepeat = "repeat";// Background repeat
}
</script>
</head>
<body>
// Page Design
</body>
<script type="text/javascript">
ChangeIt();
</script>
</html>

Share and Enjoy: Print Digg Sphinn del.icio.us Facebook Mixx Google Bookmarks Blogplay Add to favorites BarraPunto Bitacoras.com BlinkList blogmarks Blogosphere News blogtercimlap connotea Current Design Float Diggita Diigo DotNetKicks DZone eKudos email Fark Faves Fleck FriendFeed FSDaily Global Grind Gwar HackerNews Haohao HealthRanker HelloTxt Hemidemi Hyves Identi.ca IndianPad Internetmedia Kirtsy laaik.it LaTafanera LinkaGoGo LinkArena LinkedIn Linkter Live Meneame MisterWong MisterWong.DE MOB MSN Reporter muti MyShare MySpace N4G Netvibes Netvouz NewsVine NuJIJ PDF Ping.fm Posterous Propeller QQ书签 Ratimarks Rec6 Reddit RSS Scoopeo Segnalo SheToldMe Simpy Slashdot Socialogs SphereIt StumbleUpon Suggest to Techmeme via Twitter Technorati ThisNext Tipd Tumblr Twitter Upnews viadeo FR Webnews.de Webride Wikio Wikio FR Wikio IT Wists Wykop Xerpi Yahoo! Bookmarks Yahoo! Buzz Yigg 豆瓣 豆瓣九点

http://www.mfarhanonline.com/2011032622532/change-background-image-on-refresh-with-javascript/

0 comments :

Popular Posts