我有一個很奇怪的問題,函數(shù)封裝起來就出錯!
先貼代碼
<!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">低頭思故鄉(xiāng)</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>
注釋部分和沒有注釋部分的唯一區(qū)別就是將定時器中函數(shù)有無封裝,注釋中封裝成函數(shù)sceollUp就總會出錯,沒有注釋的部分也就是將函數(shù)直接寫進去,就可以正常運行,真的好奇怪,求大佬解惑