【问题标题】:jquery infinity loop to slide imagebar over and overjquery无限循环一遍又一遍地滑动图像栏
【发布时间】:2012-02-09 17:45:20
【问题描述】:

我想让脚本无限循环,这样图像每次都会旋转。这是我的脚本不起作用:

function w_gore() {
  if(document.getElementById('mycarousel').style.top != '-544px' &&  document.getElementById('up').align == 'left') {
    document.getElementById('up').align = 'right';
    $("#mycarousel").animate({"top": "-=136px"}, "slow", function() {
        document.getElementById('up').align = 'left';
    }, setTimeout(function() {ruch();},1000));
  }
}

    function ruch() {
        w_gore();
    }

$(document).ready(function(){
    ruch();
});

【问题讨论】:

  • 有什么不好的?脚本错误?提前停止?为什么你的 javascript 代码中有 <br/> 元素?

标签: jquery loops infinity


【解决方案1】:

您已经在使用 jQuery,但没有正确使用。以下是一些捷径:

document.getElementById('mycarousel').style.top

应该是:

$('#mycarousel').css('top')

document.getElementById('up').align 变为 $('#up').css('align')

document.getElementById('up').align = 'right' 变为 $('#up').css('align','right')

您还需要 Samich 的建议。

【讨论】:

    【解决方案2】:

    你需要使用setInterval:

    function w_gore() {
      if(document.getElementById('mycarousel').style.top != '-544px' &&  document.getElementById('up').align == 'left') {
        document.getElementById('up').align = 'right';
        $("#mycarousel").animate({"top": "-=136px"}, "slow", function() {
            document.getElementById('up').align = 'left';
        });
      }
    }
    
    function ruch() {
        w_gore();
    }
    
    $(document).ready(function(){
        window.setInterval(ruch, 1000);
    });
    

    附:如果您使用 jQuery - 在所有代码部分中使用它。

    jQuery 版本:

    function w_gore() {
        if ($('#mycarousel').position().top != '-544px' && $('#up').css('align') == 'left') {
            $('#up').css('align', 'right');
            $("#mycarousel").animate({ "top": "-=136px" }, "slow", function () {
                $('#up').css('align', 'left');
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      相关资源
      最近更新 更多