Using Jquery animate() with a delay to load 2 different div
Hi,
I am new here, and I hope I post in the good section.
I'm currently working on a mini-educational game for the primary school where my children are. I have some basic html, php, mysql, but I am a newbie with jquery ...
I built a quiz, and when the schoolchildren get good answers, they win a fish to place in a virtual aquarium. The fishes are png files, and I found a great way on the web to animate them with this code
- <script type="text/javascript">
- $(document).ready(function() {
- animateDiv();
-
- });
-
- function makeNewPosition(){
-
- // Get viewport dimensions (remove the dimension of the div)
- var h = $('#global').height() - 64;
- var w = $('#global').width() - 80;
-
- var nh = Math.floor(Math.random() * h);
- var nw = Math.floor(Math.random() * w);
-
- return [nh,nw];
-
- }
-
- function animateDiv() {
- var $target = $('#a');
- var newq = makeNewPosition($target.parent());
- var oldq = $target.offset();
- var speed = calcSpeed([oldq.top, oldq.left], newq);
-
- $('#a').animate({
- top: newq[0],
- left: newq[1]
- }, speed, function() {
- animateDiv();
- });
-
- };
-
- function calcSpeed(prev, next) {
-
- var x = Math.abs(prev[1] - next[1]);
- var y = Math.abs(prev[0] - next[0]);
-
- var greatest = x > y ? x : y;
-
- var speedModifier = 0.05;
-
- var speed = Math.ceil(greatest / speedModifier);
-
- return speed;
-
- }
- </script>
<script type="text/javascript">
$(document).ready(function() {
animateDiv();
});
function makeNewPosition(){
// Get viewport dimensions (remove the dimension of the div)
var h = $('#global').height() - 64;
var w = $('#global').width() - 80;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh,nw];
}
function animateDiv() {
var $target = $('#a2');
var newq = makeNewPosition($target.parent());
var oldq = $target.offset();
var speed = calcSpeed([oldq.top, oldq.left], newq);
$('#a2').animate({
top: newq[0],
left: newq[1]
}, speed, function() {
animateDiv();
});
};
function calcSpeed(prev, next) {
var x = Math.abs(prev[1] - next[1]);
var y = Math.abs(prev[0] - next[0]);
var greatest = x > y ? x : y;
var speedModifier = 0.05;
var speed = Math.ceil(greatest / speedModifier);
return speed;
}
</script>
I have two div a like this :
- <div id="a"><img src="fishX.png" /></div>
- <div id="a2"><img src="fishY.png" /></div>
and it works perfectly, the two div are animated and the illusion is perfect !
But to be more realistic, I would like to perform something like that :
When the fish swim left to right, I would like to load a div with the fish with head at right, and when the fish swim right to left, another div with its head at left(simply with inverting the png in each div, it's not the problem).
I tried to combine a fadeOut method with the delay, but it won't work. (the fadeOut and delay works itself i, but when I combine the both method, animate and fadeOut, it won't work) I added this to display the 2 differents div :
- jQuery(document).ready(function(){
- $("#a").delay(2000).fadeOut(500);
- $("#a2").delay(3000).fadeIn(500);
- $("#a2").delay(3000).fadeOut(500);
- });
But the both div #a and #a2 are displayed ...
Any idea what should I do to display only one div at a time ?
Another solution could be to detect when the fish swim to the right into the animate function and to display the div #a and when the fish swim to the left, displaying the #a2 div... but it's too hard for my level..
Thanks in advance
Ludo