当前位置: 代码迷 >> Java Web开发 >> javascript
  详细解决方案

javascript

热度:313   发布时间:2012-08-02 13:45:19.0
javascript
<!DOCTYPE html PUBLTC "-//W3C//DTD XHML 1.0 Transitional//EN"
"http://www.3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript" >
<!--   
       //全局变量
       directX=1;//X
           directY=1;//Y
           sunX=0;
           sunY=0;

      function sunMove(){
            
               sunX+=directX;
           sunY+=directY;
                   //修改div的left和top
                   var sundiv=document.getElementById("sundiv");
           sundiv.style.top=sunY+"px";
                   sundiv.style.left=sunX+"px";
                   //X方向(offsetWidth)获得当前div的宽度
                    //window.alert(sundiv.style.top);
               
                   if(sunX+sundiv.offsetWidth>=document.body.clientWidth || sunX<=0 ){
                        directX=-directX;
                            window.alert(sundiv.style.left);
                   }
                   if(sunY+sundiv.offsetHeight>=document.body.clientHeight||sunY<=0 ){
                       directY=-directY;
               
                }
          }
          setInterval("sunMove()",10);

//-->
</script>
</head>
<body style="background-image:url('year.jpg')">
<div id="sundiv" style="position:absolute">

<img src="1.gif"/>
</div>

</body>
</html>
代码运行有错 目标是使得图片在页面上飘动 碰到边界弹回 运行时左右可以上下不可以 top的值一直在0 1 徘徊 这是什么原因:?
搜索更多相关主题的帖子: html  javascript  function  content  

----------------解决方案--------------------------------------------------------
你看是不是少了一个重新附初值,window.alert(sundiv.style.bottom);

----------------解决方案--------------------------------------------------------
  相关解决方案