最近项目需要在学习JavaScript,操练了一下,写了个简单的"I Love You"屏保:
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>I Love You!</title>
<script type="text/javascript" src="js/Util.js"></script>
<script type="text/javascript" src="js/Scheduler.js"></script>
</head>
<body>
<p id="i">I</p>
<p id="love">Love</p>
<p id="you">You</p>
</body>
</html>
Util.js
function addLoadEvent (func)
{
var oldOnLoad = window.onload;
if(typeof window.onload != "function")
{
window.onload = func;
}
else
{
window.onload = function()
{
oldOnLoad();
func();
};
}
}
Scheduler.js
var scheduler =
{
index:0,
start: function()
{
var xpos = Math.floor(Math.random() * 1600);
var ypos = Math.floor(Math.random() * 600);
var fontSize = Math.floor(Math.random() * 200);
var element = null;
scheduler.index = scheduler.index % 3;
if(scheduler.index == 0)
{
element = document.getElementById("i");
}
else if(scheduler.index == 1)
{
element = document.getElementById("love");
}
else
{
element = document.getElementById("you");
}
element.style.position = "absolute";
element.style.left = xpos + "px";
element.style.top = ypos + "px";
element.style.fontSize = fontSize + "px";
scheduler.index ++;
setTimeout(scheduler.start, 2000);
}
};
addLoadEvent(scheduler.start);