jquery无限循环一遍又一遍地滑动图像条

时间:2012-02-09 17:45:21

标签: jquery loops infinity

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

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();
});

2 个答案:

答案 0 :(得分:1)

您需要使用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);
});

P.S。如果您使用的是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');
        });
    }
}

答案 1 :(得分: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 建议的内容。

相关问题