日期:2014-05-16  浏览次数:20419 次

新手自学JS,菜鸟问题像各位大侠求助啊~~!
一个不间断滚动的JS代码,如何吧body中的JS移动到head里面呢?应该怎么改动呢?还请各位大侠指点
<body>
<div id="demo" style=" overflow:hidden; width:640px; height:240px;">
<div id="demo1">
  <img src="01.jpg" />
  <img src="02.jpg" />
  <img src="03.jpg" />
  </div>
  <div id="demo2"></div>
</div>
<script type="text/javascript">
var speed=30;
demo2.innerHTML=demo1.innerHTML;
function Marquee()
{
if(demo2.offsetTop-demo.scrollTop<=0)
{
demo.scrollTop-=demo1.offsetHeight;
}
else
{
demo.scrollTop++;
}
}
var MyMar=setInterval(Marquee,speed)
demo.onmouseover=function(){clearInterval(MyMar)}
demo.onmouseout=function(){setInterval(Marquee,speed)}

</script>
</body>

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="zh">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <script type="text/javascript">
        
        window.onload = function(){
            var speed=30;
            demo2.innerHTML=demo1.innerHTML;
            function Marquee()
            {
            if(demo2.offsetTop-demo.scrollTop<=0)
            {
            demo.scrollTop-=demo1.offsetHeight;
            }
            else
            {
            demo.scrollTop++;    
            }
            }
            var MyMar=setInterval(Marquee,speed)
            demo.onmouseover=function(){clearInterval(MyMar)}
            demo.onmouseout=function(){setInterval(Marquee,speed)}
        }
</script>
</head>
<body>
<div id="demo" style=" overflow:hidden; width:640px; height:240px;">
<div id="demo1">
  <img src="01.jpg" />
  <img src="02.jpg" />
  <img src="03.jpg" />
  </div>
  <div id="demo2"></div>
</div>
</body>
</html>

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="zh">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <script type="text/javascript">
        
        window.onload = function(){
            var speed=30;
            demo2.innerHTML=demo1.innerHTML;
            
            var MyMar=setInterval(Marquee,speed)
            demo.onmouseover=function(){clearInterval(MyMar)}
            demo.onmouseout=function(){setInterval(Marquee,speed)}
        }
        
        function Marquee()
            {
            if(demo2.offsetTop-demo.scrollTop<=0)
            {
            demo.scrollTop-=demo1.offsetHeight;
            }
            else
            {
            demo.scrollTop++;    
            }
            }
</script>
</head>
<body>
<div id="demo" style=" overflow:hidden; width:640px; height:240px;">
<div id="demo1">
  <img src="01.jpg" />
  <img src="02.jpg" />
  <img src="03.jpg" />
  </div>
  <div id="demo2"></div>
</div>
</body>
</html>