Reputation: 11107
I've been working on this for a couple of hours and am not really sure what else to do. I have 5 images which I want to rotate on the screen. If you click the text down, the images move in the correct position. However, if I click it again the images shoot off into random directions. My question is, what's wrong with my code. Or better yet, am I doing it all wrong? Is there an easier way of doing this that I can't see. I created a working jsfiddle example http://jsfiddle.net/2uPJP/13/
Relevant js code...
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);
});
});
Upvotes: 0
Views: 292
Reputation: 171669
I would approach this in a much different manner by setting all positions based on same reference such as top
and right
. I then would rotate arrays containing the positions to animate to, and options like speed and delay and use a single animation function to move each image.
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/
Demo is manual mode only
Upvotes: 1
Reputation: 52
Please see this http://jsfiddle.net/2uPJP/16/, take care about the animation queue. you attach animation to a wrong item animation queue when click twice within 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);
});
});
Upvotes: 1