<
body
id=
"win"
>
<
p
style=
"background:#ff0; width:200px; height:200px; margin-top:200px; margin-left:600px"
id=
""
></
p
>
<
script
type=
"text/javascript"
>
var
loop =
0;
//统计震动次数
var
timer;
//定时器引用
var
offx;
//水平偏移量
var
offy;
//垂直偏移量
var
dir;
//控制震动方向
timer =
setInterval(
function(){
var
win =
document.
getElementById(
"win");
if (
loop >
100)
{
clearInterval(
timer);
//震动次数超过100就停止定时器
}
dir =
Math.
random()*
10 >
5 ?
1 : -
1;
//获得震动方向
offx =
Math.
random()*
20*
dir;
offy =
Math.
random()*
20*
dir;
win.
style.
marginTop =
offx+
"px";
win.
style.
marginLeft =
offy+
"px";
loop++;
},
10)
//每隔10毫秒震动一次
<
/
script
>
</
body
>