I have the following code and fiddle:
https://jsfiddle.net/bkoenig/2k5fcj1o/60/
It is working fine and every 5 seconds two new divs will be load and will be shown randomly. so far, so good.
Now I want to change the animation. I want that the divs the user sees at the moment scroll / slide up and disappear and the new divs scroll / slide up from bottom and stop.
after 5 seconds the animation starts again. hope you understand what I mean?
$(document).ready(function() {
var divs = getRandomDivs();
fadeTheDivs(divs);
});
function getRandomDivs() {
return $("div.Image").get().sort(function() {
return Math.round(Math.random()) - 0.5;
}).slice(0, 2);
}
function fadeTheDivs(divs) {
$(divs).show('slide').delay(5000).hide('slide').promise().done(function() {
var divs = getRandomDivs();
fadeTheDivs(divs);
})
}
div.Image {
display: none;
width: 50px;
height: 50px;
background: tomato;
margin: 2px;
color: white;
line-height: 50px;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="row" style="background:red;display:flex;">
<div class="Image">1</div>
<div class="Image">2</div>
<div class="Image">3</div>
<div class="Image">4</div>
<div class="Image">5</div>
<div class="Image">6</div>
<div class="Image">7</div>
</div>
2
Answers
Okay, maybe i found a better solution for me. fade in from top is working. refresh div every 5 seconds is working.
this is the fiddle: https://jsfiddle.net/bkoenig/70x5k8hp/13/
but how would it be possible to add the fade out animation? if i just add the css class to the divs it will fadein and fadeout directley without stay for a while.
I think the best way to do this is to time it properly and adjust the keyframes (i hope this is what you mean by "stay for a while".)
So, for the class
.fade-in-from-top
you have 0,5 second for the animation to take place and the keyframes are going from 0% to 100%.if you want the fadeOut animation to happen immediately after the fadeIn. then make the class
'.fade-out-to-bottom'
have twice the value of the fadeIn animation (1 second in this case). and set the keyframes to activate from 50% till 100%.This way you ensure that as soon as the fadeIn animation ends, the fadeOut animation starts. And if you want a delay between the two animations, just set a longer time interval for fadeOut and adjust the keyframe start percentage.
a quick fix for you issue:
Hope this helps.