if语句在setInterval中不起作用

时间:2013-07-30 11:16:41

标签: javascript slideshow setinterval

我正在尝试修改此幻灯片:http://css-tricks.com/snippets/jquery/simple-auto-playing-slideshow/

我的脚本是:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#slideshow > div:gt(0)").hide();
var count = 1
setInterval(function() { 
  $('#slideshow > div:first')
    .fadeOut(1000)
    .next()
    .fadeIn(1000)
    if (count == 1) 
    {
        count++
    }
    else 
    {
        .animate({top: '-100px'}, 1000)
        count++
    }
    if (count==3) 
    {
        count=1
    }
    .end()
    .appendTo('#slideshow');
},  3000);
});
</script>

所以我试图让所有其他幻灯片对象(div)使用animate方法,但首先。

如果我删除所有那些计数,如果变量和语句消失(但将动画保留在原来的位置),那么它的工作方式使得每个div都能动画化。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#slideshow > div:gt(0)").hide();
setInterval(function() { 
    $('#slideshow > div:first')
    .fadeOut(1000)
    .next()
    .fadeIn(1000)
    .animate({top: '-100px'}, 1000)
    .end()
    .appendTo('#slideshow');
},  3000);
});
</script>

如果重要,此脚本包含在cmsms页面中。

这是我的第一个javascript,可能解释了很多......

1 个答案:

答案 0 :(得分:0)

这是一个快速解决方案:

<script>
    $(function() {

        $("#slideshow > div:gt(0)").hide();

        var
            count = 1;

        setInterval(function() { 
          $('#slideshow > div:first')
            .fadeOut(1000, function () {
                if(count != 1)
                    $(this).css('top', '10px');

                count++;
            })
            .next()
            .fadeIn(1000, function() {
                if(count != 1) 
                    $(this).animate({top: '-100px'}, 1000);

                if(count == 3)
                    count = 0;
            })
            .end()
            .appendTo('#slideshow');
        },  3000);

    });
</script>
相关问题