设置div为绝对定位
position:absolute;

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> new document </title>
    <style type="text/css">
    div{
       position:absolute;
       width:200px;height:200px;border:1px solid red;
     }
    </style>
    </head><body>
    <div id="div1">ghjgj</div>
    <script type="text/javascript">
      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();
    </script>
    </body>
    </html>