一个简单的JS模拟屏保效果

  最近两天较为系统复习了下js和dom,感觉各种属性好多,还是要靠手册来活着,要不还是记不住,脑袋不行了......

  一个小东东聊以自娱,还是蛮不错的。

  这里是html页面

<html>
<head>
<script type="text/javascript" src="js.js">
</script>
</head>
<body>
<input type="button" value="createWindow!" onclick="createSmallWindow()"/>
<p/>
<input type="button" value="moveTheWindow!" onclick="moveTheWindow()"/>
<input type="button" value="speedUp" onclick="speedUp()"/>
<input type="button" value="speedDown" onclick="speedDown()"/>
<p/>
<input type="button" value="destroyWindow" onclick="destroyWindow()" />
<div id="divShow"></div>
</body>
</html>

  这个是js

/*
* global variable
*/
var myWindow;
var timer;
var speed=10;
var screenX;
var screenY;
var windowX=200;
var windowY=200;
//the window can move 0~X,0~Y
var spanX;
var spanY;
//the window location now!
var X;
var Y;
//direction 1 or -1
var directionX;
var directionY;

//create small window for test
function createSmallWindow(){
if(myWindow==undefined)
{
myWindow=window.open('','','height='+windowY+',width='+windowX);
myWindow.document.write("The window is created for test only!");
getTheScreenWidthAndHeight();
getTheInitDirection();
}
}
function getTheScreenWidthAndHeight(){
screenX=screen.width;
screenY=screen.height;
spanX=screenX-windowX-70;
spanY=screenY-windowY-40;
X=parseInt(Math.random()*screenX);
Y=parseInt(Math.random()*screenY);
myWindow.moveTo(X,Y);
}
function getTheInitDirection(){
var num=parseInt(Math.random()*10);
if(num%2==0)
{
directionX=1;
}
else
{
directionX=-1;
}
num=parseInt(Math.random()*10);
if(num%2==0)
{
directionY=1;
}
else
{
directionY=-1;
}
}
function moveTheWindow(){
if(timer!=undefined)
return;
//timer=setInterval("myWindow.moveBy(1,1);",speed);
timer=setInterval("moveIt()",speed);
}
function stopTimer(){
clearTimeout(timer);
timer=undefined;
}
function destroyWindow(){
stopTimer();
myWindow.close();
myWindow=undefined;
}
function speedUp(){
//speed=parseInt(speed/2);
//timer=setInterval("myWindow.moveBy(1,1);",speed);
directionX*=2;
directionY*=2;
}
function speedDown(){
directionX/=2;
directionY/=2;
}
//move the window function between the max X and Y
function moveIt(){
if(0<X && X<spanX){
X+=directionX;
}
else{
directionX=directionX*(-1);
X+=directionX;
}
if(0<Y&&Y<spanY){
Y+=directionY;
}
else{
directionY=directionY*(-1);
Y+=directionY;
}
myWindow.moveTo(X,Y);
}

  高手不要见笑啊!

PS:我在ubuntu中和windows中都用chrome来做过测试,基本上如果在windows创建10几20个小的window,并且让其随机出现,那么windows基本就是感觉相当的卡了,移动窗口都很卡,但是在ubuntu中,却基本不会有影响,看来windows的内存果然是拿来看的! :)

  

转载于:https://www.cnblogs.com/green-4984/archive/2011/12/18/2291723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值