0

我正在使用 div 刷新脚本(如下所示)。div 的内容包含一个自动滚动 ul(来自http://www.dynamicdrive.com/的代码)。刷新工作正常。但是刷新后自动滚动不起作用

刷新代码

    <script type="text/javascript">
        window.onload = setupRefresh;
        function setupRefresh()
        {
            setInterval("refreshBlock();",1000);
        }

        function refreshBlock()
        {
            $('#list4').load("refreshpage");

        }
    </script>

自动滚动代码

    <script type="text/javascript">

        var delayb4scroll=2000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
        var marqueespeed=1 //Specify marquee scroll speed (larger is faster 1-10)
        var pauseit=1 //Pause marquee onMousever (0=no. 1=yes)?

        var copyspeed=marqueespeed
        var pausespeed=(pauseit==0)? copyspeed: 0
        var actualheight=''

        function scrollmarquee(){
            if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
                cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
            else
                cross_marquee.style.top=parseInt(marqueeheight)+8+"px"
        }

        function initializemarquee(){
            cross_marquee=document.getElementById("vmarquee")
            cross_marquee.style.top=0
            marqueeheight=document.getElementById("list4").offsetHeight
            actualheight=cross_marquee.offsetHeight
            if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
                cross_marquee.style.height=marqueeheight+"px"
                cross_marquee.style.overflow="scroll"
                return
            }
            setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
        }

        if (window.addEventListener)
            window.addEventListener("load", initializemarquee, false)
        else if (window.attachEvent)
            window.attachEvent("onload", initializemarquee)
        else if (document.getElementById)
            window.onload=initializemarquee


    </script>

有人可以帮忙吗?

4

3 回答 3

0

您只需:

function refreshBlock()
        {
            $('#list4').load("refreshpage");
            initializemarquee();

        }
于 2012-11-01T12:58:44.760 回答
0

似乎您需要initializemarquee()在加载完成后调用。您可以在.load()'s 回调中执行此操作。

function refreshBlock(){
  $('#list4').load("refreshpage", function(){
    clearInterval(lefttime);
    initializemarquee()
  });
}

我差点忘了补充一点,你也想停止那个间隔。

于 2012-11-01T13:01:14.883 回答
0

为什么混合使用纯 JS 和 jQuery?如果你有 jQuery 使用它

这是我的重写。未经测试,但除了拼写错误或我认为可以在 jQuery 中完成但不能完成的事情之外,它应该完成整个事情

$(function() {
  var sId = setInterval(function {
    $('#list4').load("refreshpage");
  },1000);
  var $cross_marquee=$("#vmarquee")
  var delayb4scroll=2000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
  var marqueespeed=1 //Specify marquee scroll speed (larger is faster 1-10)
  var pauseit=true //Pause marquee onMousever (false=no. true=yes)?
  var copyspeed=marqueespeed;
  var pausespeed=(pauseit==0)? copyspeed: 0;
  var actualheight=$cross_marquee.height();
  var marqueeheight=$("#list4").height();
  $cross_marquee.top(0);

  if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){ //if Opera or Netscape 7x, add scrollbars to scroll and exit
    $cross_marquee.height(marqueeheight);
    $cross_marquee.css("overflow","scroll");
  }
  else var tId = setTimeout(function() { 
    lefttime=setInterval(
    function() { 
      var top = $cross_marquee.top(); 
      if (top>(actualheight*(-1)+8)) $cross_marquee.top(top-copyspeed)
      else $cross_marquee.top(marqueeheight+8);
     }

    },30)
    , delayb4scroll);

});
于 2012-11-01T13:13:48.063 回答