How to Use Jquery to Wait For the End of Css3 Transitions

How to use jQuery to wait for the end of CSS3 transitions?

For transitions you can use the following to detect the end of a transition via jQuery:

$("#someSelector").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });

Mozilla has an excellent reference:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions#Detecting_the_start_and_completion_of_a_transition

For animations it's very similar:

$("#someSelector").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){ ... });

Note that you can pass all of the browser prefixed event strings into the bind() method simultaneously to support the event firing on all browsers that support it.

Update:

Per the comment left by Duck: you use jQuery's .one() method to ensure the handler only fires once. For example:

$("#someSelector").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });

$("#someSelector").one("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){ ... });

Update 2:

jQuery bind() method has been deprecated, and on() method is preferred as of jQuery 1.7. bind()

You can also use off() method on the callback function to ensure it will be fired only once. Here is an example which is equivalent to using one() method:

$("#someSelector")
.on("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd",
function(e){
// do something here
$(this).off(e);
});

References:

  • .off()

  • .one()

jQuery should wait until css transition

CC3 generates DOM events, so you can sing on TransitionEnd event.
See more info on this answer.

How to get jQuery to wait until an animation is finished?

Run a conditional check first to determine if the trigger class flipped has already been added to the element in question.

This will imply that the animation is still running or currently active (if the class flipped is already present).

if (!$(this).find(".card").hasClass('flipped')) {
$(this).find(".card").toggleClass('flipped')
}

$(document).ready(function() {
$(".container").hover(function() {
if (!$(this).find(".card").hasClass('flipped')) {
$(this).find(".card").toggleClass('flipped')
}

}, function() {
var val = $(this).find(".card")
setTimeout(function() {
val.removeClass('flipped')
}, 1000);
});
});
.container {
width: 200px;
height: 260px;
position: relative;
margin: 0 auto 40px;
-webkit-perspective: 800px;
-moz-perspective: 800px;
-o-perspective: 800px;
perspective: 800px;
display: inline-block;
}

#main {
border: 1px solid black;
}

button {
width: 30%;
height: 10%;
margin-top: 100px;
cursor: default;
}

.card {
width: 100%;
height: 100%;
position: absolute;
-webkit-transition: -webkit-transform 1s;
-moz-transition: -moz-transform 1s;
-o-transition: -o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
-webkit-transform-origin: right center;
-moz-transform-origin: right center;
-o-transform-origin: right center;
transform-origin: right center;
}

.card.flipped {
-webkit-transform: translateX( -100%) rotateY( -180deg);
-moz-transform: translateX( -100%) rotateY( -180deg);
-o-transform: translateX( -100%) rotateY( -180deg);
transform: translateX( -100%) rotateY( -180deg);
}

.card div {
height: 100%;
width: 100%;
color: white;
text-align: center;
font-weight: bold;
position: absolute;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden;
cursor: pointer;
}

.card .front {
background: red;
display: flex;
justify-content: center;
align-items: center;
}


/*
.card .front p {
margin-top: 100px;
}
*/

.card .back p {
margin: auto;
}

.card .back {
background: blue;
-webkit-transform: rotateY( 180deg);
-moz-transform: rotateY( 180deg);
-o-transform: rotateY( 180deg);
transform: rotateY( 180deg);
display: flex;
justify-content: center;
align-items: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<div id="main"><br>
<section class="container">
<div class="card">
<div class="front">
<p>Test</p>
</div>
<div class="back">
<p>MyBack</p>
</div>
</div>
</section>
</div>
</div>

How do I wait for this animation to finish before moving on to the next one?

To elaborate on entio's answer, your animation occurs when an element has the class "typing-effect." When that animation ends, the browser calls an event called "animationend." You can use JavaScript to run your animation on the next element by accessing that event.

Notice in HTML the snippit below, I've moved the "display: hidden" to a CSS class and removed the "typing-effect." In my JavaScript function, I enable the class in the first element, increment the counter, and told the "animationend" to call the function again with the new value for "i."

Edit: I forgot to mention, I modified the id values. I don't believe a valid id value in HTML5 can contain parenthesis.

console.log("script.js connected");

let iLevel = 1;
let loop = 0;

function animateNext(i){
let stop = document.querySelectorAll('.animated').length;
let animated = document.querySelector(`#h2_${i}`);
animated.classList.add('typing-effect');

animated.addEventListener('animationend', () => {
if(i===stop){
iLevel = "iDone";
}else{
animateNext(i+1);
}
});
}

function startGame() {
animateNext(1);
}
.animated{
display: none;
}
.typing-effect {
display: block;
animation: typing-effect 5s steps(130, end), 0.75s step-end infinite;
white-space: nowrap;
overflow: hidden;
border-right: 2px solid black;
}
.typing-effect:after {
content: " ";
}
@keyframes typing-effect {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
@keyframes blink-caret {
from,
to {
border-color: transparent;
}
50% {
border-color: black;
}
}
<button onclick="startGame();">START GAME</button>

<div id="instructions">

<div>
<h2 id="h2_1" class="animated">Welcome to The Number Wizrd</h2>
</div>

<div>
<h2 id="h2_2" class="animated">Adjfosdf</h2>
</div>
<div>
<h2 id="h2_3" class="animated">sosidjfs</h2>
</div>
<div>
<h2 id="h2_4" class="animated">difjspodf</h2>
</div>
<div>
<h2 id="h2_5" class="animated">skidjfosidf</h2>
</div>

</div>

How to pause and restart a css3 transition in jquery transit

You were most of the way there with .stop() I just added a little logic to tell it what to do when it stops.

Working Example

$('button').click(function () {
if (!$('.box').hasClass('stop')) { // if the box does not have class "stop"
$('.box').stop().transition({ // stop the transition
x: $('.box').offset().left + $('.box').width() / 2 // where the box should be when it stops
}, 1).addClass('stop'); // simple add class for easy button control
} else { // if the box has class "stop"
$('.box').transition({
x: '350px'
}, 5000).removeClass('stop'); // continue with regularly scheduled programming then remove "stop"
}
});

Using jQuery promise to wait for transitions

jQuery promises only work on animations performed by jQuery, and not on CSS transitions.

You can either:

  1. use jQuery itself to perform the animations (most portable), or

  2. catch the transitionend event - demo at http://jsfiddle.net/alnitak/v7vn94ea/

See https://developer.mozilla.org/en-US/docs/Web/Events/transitionend for more information on the latter.

jQuery: wait for css animation to complete before changing another css attribute

You need to apply the z-index:-1 after it has animated back to opacity 0, otherwise it is just "popping" below without showing the opacity change.

You need to trigger on a transitionEnd event. See this SO post about normalizing that event for all browsers.

But once you work that out, its basically just attaching a one off event with the callback setting the final z-index - something like this:

visible.one('transitionEnd', function(){
visible.css('z-index', -1);
}).css( 'opacity', 0 );

You just need to use the script in the other SO post to get your normalized 'transitionEnd' event.

Hope this helps!



Related Topics



Leave a reply



Submit