2013-07-11 51 views
-1

你好,我再次发现一个伟大的脚本,几乎可以做到我想要的问题是,当页面加载时,它不会自行移动,我们可以改变它来做到这一点? 这个想法是使滚动下来的消息,我想使它成为一个循环,所以我有一条消息,它会从上到下移动,但它永远不会停止移动像一个跑马灯 在此先感谢!marque like with javascript

<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript"> 

scrollStep=1 

timerUp="" 
timerDown="" 

function toTop(id){ 
document.getElementById(id).scrollTop=0 
} 

function scrollDivDown(id){ 
clearTimeout(timerDown) 
document.getElementById(id).scrollTop+=scrollStep 
timerDown=setTimeout("scrollDivDown('"+id+"')",10) 
} 

function scrollDivUp(id){ 
clearTimeout(timerUp) 
document.getElementById(id).scrollTop-=scrollStep 
timerUp=setTimeout("scrollDivUp('"+id+"')",10) 
} 

function toBottom(id){ 
document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight 
} 

function stopMe(){ 
clearTimeout(timerDown) 
clearTimeout(timerUp) 
} 

</script> 

<style> 
#display{ 
width:200px; 
height:150px; 
overflow:hidden; 
text-align:left; 
border:1px solid black; 
} 
</style> 
</head> 

<body> 
<a href="#null" onclick="toTop('display')">Top</a> 
<a href="#null" onmouseover="scrollDivDown('display')" onmouseout="stopMe()">ScrollDown</a> 
<a href="#null" onmouseover="scrollDivUp('display')" onmouseout="stopMe()">Scroll Up</a> 
<a href="#null" onclick="toBottom('display')">Bottom</a> 
<div id="display"> 

<b>LAYER CONTENTS</b> 

<P>Dummy Text<P>Dummy Text<P>Dummy Text<P>Dummy Text 
<P>Dummy Text<P>Dummy Text<P>Dummy Text<P>Dummy Text 
<P>Dummy Text<P>Dummy Text<P>Dummy Text<P>Dummy Text 
<P>Dummy Text<P>Dummy Text<P>Dummy Text<P>End 

</div> 
</body> 
</html> 

我想要做这样的事情所以它会使一个循环:

function scrollDown(id){ 
clearTimeout(timerDown) 
document.getElementById(id).scrollTop+=scrollStep 
if (document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight){ 
    document.getElementById(id).scrollTop=0 
    } 

} 






<div id="display" onmouseout="scrollDown('display')" onmouseover="stopMe()"> 

,并在这个div在你的代码的消息

+0

页面加载时应该怎么做?你期望什么行为? – Sergio

+0

我希望它自己从底部移动到顶部,而不需要靠近鼠标或点击任何东西 –

+0

这是您要找的吗? http://jsfiddle.net/QGveQ/ – Sergio

回答

0

尝试在window.onload要运行在页面加载。

window.onload = function() { ...yourcode... };

0

你可以试着来包装你的代码为以下方式进入

windows.load =函数(){//你的代码}

由于脚本在文档加载之前加载

<script type="text/javascript"> 
window.load = function(){ 
scrollStep=1 

timerUp="" 
timerDown="" 

function toTop(id){ 
document.getElementById(id).scrollTop=0 
} 

function scrollDivDown(id){ 
clearTimeout(timerDown) 
document.getElementById(id).scrollTop+=scrollStep 
timerDown=setTimeout("scrollDivDown('"+id+"')",10) 
} 

function scrollDivUp(id){ 
clearTimeout(timerUp) 
document.getElementById(id).scrollTop-=scrollStep 
timerUp=setTimeout("scrollDivUp('"+id+"')",10) 
} 

function toBottom(id){ 
document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight 
} 

function stopMe(){ 
clearTimeout(timerDown) 
clearTimeout(timerUp) 
} 
}; 

</script>