日期:2014-05-17  浏览次数:20601 次

Dom中利用setInterval实现走马灯问题
要实现功能:实现标题栏走马灯效果,按【向左】按钮就向左连续滚动,按【向右】按钮就像有连续滚动。
自己写的代码:
[code=HTML][/code]<head>
  <title>新学期欢迎新同学!!</title>
  <script type="text/javascript">
  function zuozou() {
  var title = document.title;
  var f = title.charAt(0);
  var q = title.substring(1, title.length);
  document.title = q + f;
  }
  function youzou() {
  var title = document.title;
  var f = title.charAt(9);
  var q = title.substring(0, title.length - 1);
  document.title = f+q;
  }
  var setinterval1 = setInterval("zuozou()", 2000);
  var setinterval2 = setInterval("youzou()", 2000);
  </script>
</head>
<body>
<input type="button" value="左滚" onclick="clearInterval(setinterval2);setinterval1" />
<input type="button" value="右滚" onclick="clearInterval(setinterval1);setinterval2" />
</body>

------解决方案--------------------
JScript code
<head>
  <title>新学期欢迎新同学!!</title>
  <script type="text/javascript">
  function zuozou() {
  var title = document.title;
  var f = title.charAt(0);
  var q = title.substring(1, title.length);
  document.title = q + f;
  }
  function youzou() {
  var title = document.title;
  var f = title.charAt(9);
  var q = title.substring(0, title.length - 1);
  document.title = f+q;
  }
var setinterval1 =null
var setinterval2 =null

function scroll(m)
{
if(m==1)
{
if(setinterval2)
clearInterval(setinterval2)
setinterval1=setInterval("zuozou()", 200);
}
else
{
if(setinterval1)
clearInterval(setinterval1)
setinterval2=setInterval("youzou()", 200);
}
}
  </script>
</head>
<body>
<input type="button" value="左滚" onclick="scroll(1)" />
<input type="button" value="右滚" onclick="scroll(2)" />
</body>