日期:2014-05-16 浏览次数:20323 次
div{
width:200px;height:200px;border:1px solid red;
}
var i=0;
var j=0;
var k=0;
var f=20;
var interval;
var v=document.getElementById("div1");
var h=document.documentElement.clientHeight;
document.getElementById("div1").style.top=h;
function bian(){
var heightClient=document.documentElement.clientHeight;
var widthClient=document.documentElement.clientWidth;
var leftdiv=v.offsetLeft;
var topdiv=v.offsetTop;
var widthdiv=v.offsetWidth;
var heightdiv=v.offsetHeight;
v.style.top=h+document.body.scrollTop+"px";
v.style.left=f+document.body.scrollLeft+"px";
if(j){
h=h+1;
}else{
h=h-1;
}
if(h<0){
j=1;h=0;
}
if(h>heightClient-heightdiv){
j=0;
h=heightClient-heightdiv;
}
if(i){
f=f+1;
}else{
f=f-1;
}
if(f<0){
i=1;f=0;
}
if(f>=(widthClient-widthdiv)){
i=0;
f=widthClient-widthdiv;
}
}
function fff(){
v.style.visibility="visible";
interval=setInterval("bian()",30);
}
fff();
<div id="div1">ghjgj</div>