通过一组带动画的图像循环

时间:2012-11-22 01:43:25

标签: javascript jquery html css jquery-animate

我一直在研究这个问题几个小时,我不确定还能做些什么。我有5张图像,我想在屏幕上旋转。如果单击文本,图像将移动到正确的位置。但是,如果我再次点击它,图像就会随机拍摄。我的问题是,我的代码出了什么问题。或者更好的是,我做错了吗?有没有更简单的方法来做到这一点,我无法看到。我创建了一个有效的jsfiddle示例http://jsfiddle.net/2uPJP/13/

相关的js代码......

function convert () {

  var $hide1 = $(".hide1");

  var $hide2 = $(".hide2");

  var $hide3 = $(".hide3");

  var $hide4 = $(".hide4");

  var $hide5 = $(".hide5");

  $hide1.removeClass().addClass("hide2");

  $hide2.removeClass().addClass("hide3");

  $hide3.removeClass().addClass("hide4");

  $hide4.removeClass().addClass("hide5");

  $hide5.removeClass().addClass("hide1");

}

$(document).ready(function() {

  $('.hide1').animate({
    top: '+=100',
    right: '+=100'  
  }, 1500);  

  $('.hide5').animate({
    bottom: '+=100',
    right: '+=100'   
  }, 1500);  


$('.down').click(function() {

  $('.hide1').animate({
    left: '+=100'  
  }, 1500);  

  $('.hide2').animate({
    top: '+=100'  
  }, 1500);  

  $('.hide3').animate({
    top: '+=100'  
  }, 1500);  

  $('.hide4').animate({
    right: '+=100'  
  }, 1500);  

  $('.hide5').animate({
    bottom: '+=200'  
  }, 1500);  

  setTimeout(convert, 1501);

});
});

2 个答案:

答案 0 :(得分:1)

请参阅此http://jsfiddle.net/2uPJP/16/,注意动画队列。在1500

中单击两次时,您将动画附加到错误的项目动画队列
function convert() {
            var $hide1 = $(".hide1");

            var $hide2 = $(".hide2");

            var $hide3 = $(".hide3");

            var $hide4 = $(".hide4");

            var $hide5 = $(".hide5");

            $hide1.removeClass("hide1").addClass("hide2");

            $hide2.removeClass("hide2").addClass("hide3");

            $hide3.removeClass("hide3").addClass("hide4");

            $hide4.removeClass("hide4").addClass("hide5");

            $hide5.removeClass("hide5").addClass("hide1");

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

            $('.hide1').animate({
                top: '+=100',
                left: '-=100'
            }, 1500);

            $('.hide5').animate({
                top: '-=100',
                left: '-=100'
            }, 1500);


            $('.down').click(function () {
                convert()
                $('.hide2').animate({
                    left: '+=100'
                }, 1500);

                $('.hide3').animate({
                    top: '+=100'
                }, 1500);

                $('.hide4').animate({
                    top: '+=100'
                }, 1500);

                $('.hide5').animate({
                    left: '-=100'
                }, 1500);

                $('.hide1').animate({
                    top: '-=200'
                }, 1500);
            });

        });

答案 1 :(得分:1)

我会通过基于相同参考设置所有位置(例如topright)以非常不同的方式处理此问题。然后,我将旋转包含要设置动画的位置的数组,以及速度和延迟等选项,并使用单个动画功能移动每个图像。

var positions = [ /*[ top,right]*/
     [100, 100], [100, 0], [200, 0], [300, 0], [300, 100]
 ];

var animOpts=[/* delay, speed*/
    [300,1500],[0,1500],[0,1500],[300,1500],[0,2000]
  ]


var $fish;

$(document).ready(function() {

    $fish = $('.span2 img');
    /* move 1st and 5th on page load */
    fishAnim(0);
    fishAnim(4);

    $('.down').click(function() {
        rotateAllfish();
    });

});

function shuffleArray( arr){ 
    /* move element in first position to end of array */
    arr.push( arr.shift());
}

function rotateAllfish() {    
    shuffleArray( positions );

    $fish.each(function(fishIndex) {
        fishAnim(fishIndex, true)
    });

     shuffleArray(animOpts);

}

function fishAnim(fishIndex, addDelay) {
    var delay= addDelay ? animOpts[fishIndex][0] : 0;
    $fish.eq(fishIndex).delay( delay ).animate({
        top: positions[fishIndex][0],
        right: positions[fishIndex][1]
    }, animOpts[fishIndex][1]);
}

DEMO:http://jsfiddle.net/2uPJP/19/

演示仅限手动模式