之前也沒有對這個方法有什么疑惑,直到......先貼代碼<!DOCTYPE?html>
<html>
<head>
????<meta?charset="utf-8"?/>
????<meta?http-equiv="X-UA-Compatible"?content="IE=edge">
????<title>無縫滾動</title>
????<meta?name="viewport"?content="width=device-width,?initial-scale=1">
????<style>
????????*{padding:?0;margin:?0;}
????????ul,li{list-style:?none;}
????????div{width:?150px;height:?22px;background:?#eee;overflow:?hidden;}
????????ul{width:?100px;margin-left:?20px;}
????</style>
</head>
<body>
????<div?id="area">
????????<ul?id="ul1">
????????????<li?style="color:?red">床前明月光</li>
????????????<li?style="color:?blue">疑是地上霜</li>
????????????<li?style="color:?yellow">舉頭望明月</li>
????????????<li?style="color:?green">低頭思故鄉</li>
????????</ul>
????????<ul?id="ul2"></ul>
????</div>
<script>
//?var?area=document.getElementById("area");
//?var?ul1=document.getElementById("ul1");
//?var?ul2=document.getElementById("ul2");
//?ul2.innerHTML=ul1.innerHTML;
//?function?scrollUp(){
//?????if(area.scrollTop?>=?ul1.offsetHeight){
//?????????area.scrollTop=0;
//?????}else{
//?????????area.scrollTop++;
//?????}
//?}
//?var?myscroll=setInterval(scrollUp(),50);
//?area.onmouseover=function(){
//?????clearInterval(myscroll);
//?}
//?area.onmouseout=function(){
//?????myscroll=setInterval(scrollUp(),50);
//?}
????var?area=document.getElementById("area");
????var?ul1=document.getElementById("ul1");
????var?ul2=document.getElementById("ul2");
????ul2.innerHTML=ul1.innerHTML;
????var?myscroll=setInterval(function(){
????????if(area.scrollTop?>=?ul1.offsetHeight){
????????????area.scrollTop=0;
????????}else{
????????????area.scrollTop++;
????????}
????},50);
????area.onmouseover=function(){
????????clearInterval(myscroll);
????}
????area.onmouseout=function(){
????????myscroll=setInterval(function(){
????????????if(area.scrollTop?>=?ul1.offsetHeight){
????????????????area.scrollTop=0;
????????????}else{
????????????????area.scrollTop++;
????????}
????},50);
}
</script>
</body>
</html>? ? ? 注釋部分和沒有注釋部分的唯一區別就是將定時器中函數有無封裝,注釋中封裝成函數sceollUp就總會出錯,沒有注釋的部分也就是將函數直接寫進去,就可以正常運行,真的好奇怪,求大佬解惑,感謝!
有一個關于setInterval的問題
專注程序不可自拔
2018-11-22 13:40:05