Animation doesn't complete with jQuery TransitionEnd event - javascript

Situation
.rightnav.front is clicked for DIV1.
DIV1 moves to the right, and opacity is lowered
DIV1 moves back left, and at completion opacity is 0
DIV1 gets class .hidden and .offset to hide it and to move it off screen, so it's not clickable anymore..
The next DIV (DIV with id 2 for test-purposes) has it's hidden and offset classes removed, and is the target for the next click-event.
Problem
The DIV moves right, but doesn't move left (back again) before it's hidden. See Codepen at the bottom for a try-out.
I'm only posting the JavaScript code here.. CSS and HTML you'll find in the codepen.
Here is the jQuery code
$(document.body).on('click','.rightnav.front', function () {
var x = $(this).parent().parent();
x.addClass('moveright')
.one('transitionend', function() {
x.removeClass('moveright')
})
.one('transitionend', function(){
x.addClass('moveleft');
})
.one('transitionend', function() {
x.addClass('hidden').addClass('offset');
$('.rightnav.front').removeClass('front');
var nextId = Number(x.attr('id')) + 1;
var nextWidget = $('#' + nextId);
nextWidget.removeClass('hidden');
nextWidget.children().find('.rightnav').addClass('front');
})
});
CodePen
Here is the CodePen.IO link for a live test: http://codepen.io/nygter/pen/QbpegM

Take a look at this solution, maybe (sure) it's not ideal, but should work as expected. As I mentioned in comment I've moved animation from jQuery to #keyframes.
Magic cames from:
.widget.moveright{
left:450px;
margin-left:-100px;
opacity:0.5;
}
and
x.addClass('moveright')
.one('transitionend', function() {
x.removeClass('moveright')
})
.one('transitionend', function(){
x.addClass('moveleft');
}) //...
Transformed into:
#keyframes moveright{
50% {
left:450px;
margin-left:-100px;
opacity:0.5;
}
100% {
opacity: 0;
}
}
.widget.moveright{
animation: moveright 1s ease;
-webkit-animation: moveright 1s ease;
}
and
x.addClass('moveright')
.one('animationend', function() {
$(this)
.removeClass('moveright')
.addClass('hidden offset');
//...
See it in action at Codepen.
To understand CSS animations take a look.

Related

Css animation - stop and revert animation

I want my custom element to move to the right for 200px when clicking a button. So the element starts moving on click. When in the animation I want to be able to click the button again to stop and let the element have an animated return to initial state (reverse animation from the current state).
I accomplished this with css transitions:
var state = false;
var button = document.getElementById("but");
button.onclick = clickA;
function clickA(){
state = !state;
var el = document.getElementById("el");
if (state) el.setAttribute("class", "animatedElement open");
else el.setAttribute("class", "animatedElement");
}
.animatedElement {
padding-left: 0px;
transition: padding-left 5s;
}
.open {
padding-left: 200px;
}
<button id="but">Click</button>
<div id="el">element</div>
So on click I just toggle the .open class on the component.
I want to know if this is possible to do with css animations? I tried this:
var state = false;
var button = document.getElementById("but");
button.onclick = clickA;
function clickA(){
state = !state;
var el = document.getElementById("el");
if (state) el.setAttribute("class", "animatedComponent-open");
else el.setAttribute("class", "animatedComponent-close");
}
#keyframes animationOpen {
0% {padding-left: 0px;}
100% {padding-left: 200px;}
}
.animatedComponent-open {
animation: animationOpen 3s normal forwards;
}
#keyframes animationClose {
0% {padding-left: 0px;}
100% {padding-left: 200px;}
}
.animatedComponent-close {
animation: animationClose 3s reverse forwards;
}
<button id="but">Click</button>
<div id="el">element</div>
But this doesn't revert open animation when clicked in the middle of animation, but jumps to the final state and then plays close animation from beginning.
As the keyframes have a starting point and and ending point, so they will start from those positions no matter where where the current position of the element is. Since you are using js to toggle, transitions are the way to go. Animations are just complicating it more without any benefits.
Visit https://css-tricks.com/controlling-css-animations-transitions-javascript/
I am sure this article will have the exact answer for you. There are many work arounds to get the current keyframe values at an animation playstate of pause.
Then with these values you can use the animate api to build a reverse animation.
If you can pass the reverse animation the pause keyframe values as starting-position values. Then you can set your animation end keyframes to paddingLeft:0px - just how it started.
This will solve the unwanted jumping to the end of the animation.

How to remove a div with fade out effect in JavaScript?

I want to remove a div element on click event but i want to remove it with a fade out effect. I have got some JQuery solution but i need pure JavaScript or css solution.
document.querySelector('.list').addEventListener("click", function(e){
if (e.target.localName === "span") {
var removeTarget = e.target.parentNode.parentNode;
removeTarget.parentNode.removeChild(removeTarget);
};
});
This code is removing the div element with no effect. How can i add a fade out effect?
I've made this function a while ago for a personal project:
function removeFadeOut( el, speed ) {
var seconds = speed/1000;
el.style.transition = "opacity "+seconds+"s ease";
el.style.opacity = 0;
setTimeout(function() {
el.parentNode.removeChild(el);
}, speed);
}
removeFadeOut(document.getElementById('test'), 2000);
There are two ways you can achieve this: CSS3 animation or jQuery animation.
CSS3 Animation
In your CSS document, add:
.list {
opacity: 1;
-webkit-transition: opacity 1000ms linear;
transition: opacity 1000ms linear;
}
This will make any change of opacity to your item fade by 1000ms.
Change line 4 of your JavaScript to:
removeTarget.style.opacity = '0';
setTimeout(() => removeTarget.remove(), 1000);
This will make your item change opacity to 0, thus making the transition from step 1 have an effect. Then it will remove the item with your code after 1000ms.
Note: Make sure the time of the CSS3 transition and the setTimeout are the same.
jQuery Animation
Get jQuery
Go to the jQuery Website and download it, or
Add ` in your HTML document before any jQuery code.
Change line 4 of your Javascript to:
removeTarget.fadeOut(1000)
This will Fade Out your item by 1000ms, you can change this time to whatever you want.
In 2020 you can forgo use of use setTimeout for the animationend event, removing the need to maintain the duration in two places:
.fade-out {
animation: fade 2s;
-webkit-animation: fade 2s;
-moz-animation: fade 2s;
}
/* Animate opacity */
#keyframes fade {
from { opacity: 1 }
to { opacity: 0 }
}
#-moz-keyframes fade {
from { opacity: 1 }
to { opacity: 0 }
}
#-webkit-keyframes fade {
from { opacity: 1 }
to { opacity: 0 }
}
const elementToFade = document.getElementById('my-element');
elementToFade.onanimationend = (e) => {
if (e.target.classList.contains('fade-out')) {
elementToFade.parentNode.removeChild(elementToFade);
}
};
// To fade away:
elementToFade.classList.add('fade-out');
It's a good question, but to animate some element in html, this element has to exist while it is animating. So, you have some ways to do this, a good way is hide this element with CSS and after the animation you remove this element. While you hiding you can animate, you can see this example:
<style>
.hide{
opacity: 0;
}
.fade-out {
transition:1s linear all;
}
</style>
<span class="list fade-out">
This is a List, click me to hide
</span>
<script>
document.querySelector('.list').addEventListener("click", function(e) {
if (e.target.localName === "span") {
//Add CSS hide and animate with fade out
var currentCSS = this.className;
this.className = currentCSS + ' hide';
var removeTarget = e.target.parentNode.parentNode;
setTimeout(function(){
removeTarget.parentNode.removeChild(removeTarget);
},1000);
};
});
</script>
Add the following CSS class to the element using elem.className="my-animation"; on click:
.my-animation {
animation: fade 3s steps(90) forwards;
-webkit-animation: fade 3s steps(90) forwards;
-moz-animation: fade 3s steps(90) forwards;
}
#keyframes fade {
from {
opacity: 1;
}
to {
opacity: 0.0;
}
}
You may control the speed of the animation by modifying the steps(number) as well.
Just goto jQuery source code, take out the fade code which is in pure javascript, and use it, no need to reinvent the wheel,
or a hint is reduce the height of div to 0 slowly using setTimeInterval()
or a css solution would be to use transform and transition properties

CSS fade out multiple times

I'm making a website which will let you update an SQL table, and I want to add some sort of feedback when a button is clicked. I have made an invisible button (opacity=0) which lies to the right of each row as a status. I made this JS fade() function to set the opacity to 1, then slowly bring it back to 0, so a message pops up then fades away.
function fade () {
var invis = document.getElementById("invis".concat(num.toString()));
if(invis.style.opacity > .990) {
invis.style.opacity = (invis.style.opacity) - .001;
setTimeout(fade, 50);
} else if(invis.style.opacity > 0) {
invis.style.opacity = (invis.style.opacity) - .05;
setTimeout(fade, 50);
}
}
The trouble is, since webpages are single-threaded, any other action will interrupt the animation and leave behind a half-faded status. So that's no good. So now I am trying to set up the invisible buttons to change class when a new row is updated. The new class looks like this:
.invisible_anim {
...
opacity: 0;
animation:trans 3000ms;
}
#keyframes trans {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
This works fine, except it only works once. From here I cannot get the animation to play a second time. I have tried changing the class back to "invisible" then "invisible_anim" with no luck. I also can't use JQuery or Webkit. I'm wondering if there's some flag you can set for a button without actually clicking on it so I can reset the class when I need to? Or even some way to thread my JS function so I can stick with that.
If you would like to play the animation multiple times (see docs here: https://developer.mozilla.org/en-US/docs/Web/CSS/animation), if you would like to play it twice only.
so this:
.invisible_anim {
...
opacity: 0;
animation:trans 3000ms;
}
#keyframes trans {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
would turn to
.invisible_anim {
...
opacity: 0;
animation:trans 3s 2 ;
}
#keyframes trans {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
EDIT:
Apparently the requirements are different than what I thought. Instead the solution seems to be to key off the animation event located at https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations and then when that animation done do what you need to do: so in JS-only
var e = document.getElementById("watchme");
e.addEventListener("animationend", listener, false);
function listener(){
//do what you need to do here
}
Just be careful, the reason for this is that most browsers have different "animationend" events that fire at different times. So definitely will need to be tested in different browsers to make sure that the animation event is firing at the right time. There's a post at (https://css-tricks.com/controlling-css-animations-transitions-javascript/) that details some of the issues you might encounter.
Have you considered using the CSS property "transition"? JavaScript has an event listener called "transitionend" that can trigger when your transition has ended, which you can use to reset the button.
First set the area for your alert button with the id invis.
CSS:
#invis {
opacity: 1;
transition: opacity 3s;
}
Then in JavaScript, generate your button and its content, which will appear at opacity 1, then transition to opacity 0. Your addEventListener will trigger when the animation is done, remove the button and reset the opacity for the next trigger.
JavaScript:
var invis = getElementByID("invis");
function fade() {
var button = document.createElement("button");
invis.appendChild(button);
invis.style.opacity = ("0");
invis.addEventListener("transitionend", function(){
invis.removeChild(button);
invis.style.opacity = ("1");
});
}
You can add the fade() function to your EventListener for the user "click."
This is my first time answering on StackOverflow, I hope this helps!
You need to start transparent then show then hide:
#keyframes trans {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
Then simply add your class (remove after the 3000ms time period)

Toggling jQuery animation not working

Trying to do a simple animation on click. I have a panel thats hidden off to the right side (absolute positioned) when the user clicks the tab i want the panel to slide out. I can get the initial slide out to work but cant get the toggle to function properly
$('#sideTab').click(function() {
$('#sideCol').animate({'right':'0%'})
}, function ()
$('#sideCol').animate({'right':'-50%'})
});
$('#sideTab').toggle(function() {
$('#sideCol').animate({'right':'0%'})
}, function ()
$('#sideCol').animate({'right':'-50%'})
});
neither of these are working
fiddle: http://jsfiddle.net/BQE32/1/
when the green square is clicked the blue square should move left, and when its clicked again it should move back to original positioning
click doesn't accept two callback functions, you can read the right property and set the proper value:
$('#sideTab').click(function () {
var $e = $('#sideCol');
$e.animate({
'right': $e.css('right') === '0px' ? '-50%' : '0px'
});
});
http://jsfiddle.net/Jrs36/
You can also use CSS transition property and jQuery .toggleClass() method:
CSS:
#sideCol {
-webkit-transition: right 400ms;
-moz-transition: right 400ms;
-o-transition: right 400ms;
transition: right 400ms;
}
#sideCol.right50 {
right: 50%;
}
JavaScript:
$('#sideTab').click(function () {
$('#sideCol').toggleClass('right50');
});
$('#sideTab').on('click', function() {
if ($(this).hasClass('toggled') === true) {
$(this).animate({'right': '-50%'}).removeClass('toggled');
} else {
$(this).animate({'right': '0'}).addClass('toggled');
}
});
This will check if the sidebar is already 'toggled', and if it is will remove the class and animate it back out of the frame (and vice versa).
You can use toggle() here:
$('#sideTab').toggle(function() {
$('#sideCol').animate({'right':'0%'})
}, function() {
$('#sideCol').animate({'right':'-50%'})
});
Demo

Javascript slide effect onclick

I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick".
The code is here: http://jsfiddle.net/TCUd5/
The DIV that has to slide has id="pulldown_contents_wrapper".
This DIV is contained in a SPAN, that also triggers it:
<span onclick="toggleUpdatesPulldown(event, this, '4');" style="display: inline-block;" class="updates_pulldown" >
<div class="pulldown_contents_wrapper" id="pulldown_contents_wrapper">
And I think the JS code that controls the SPAN onclick is:
var toggleUpdatesPulldown = function(event, element, user_id) {
if( element.className=='updates_pulldown' ) {
element.className= 'updates_pulldown_active';
showNotifications();
} else {
element.className='updates_pulldown';
}
}
If it is not possible to make it with pure JS, do you have an idea how could I do it with Mootools? (*I'd like to use only pure JS or the Mootols framework).
I have tried to implement the code from: why javascript onclick div slide not working? but with no results.
Thanks a lot.
I have managed to make it with Mootools, but I can't figure it out how to add a slide & fade effect, and a delay on mouseout
window.addEvent('domready', function() {
$('updates_pulldown').addEvents({
mouseenter: function(){
$('updates_pulldown').removeClass('updates_pulldown').addClass('updates_pulldown_active')
$('pulldown_contents_wrapper').set('tween', {
duration: 1000,
physics: 'pow:in:out',
transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
}).show();
},
mouseleave: function(){
$('pulldown_contents_wrapper').set('tween', {
duration: 1000,
delay: 1000,
}).hide();
$('updates_pulldown').removeClass('updates_pulldown_active').addClass('updates_pulldown')
},
});
});
var toggleUpdatesPulldown = function(event, element, user_id) {
showNotifications();
}
Any idea?
jQuery is a lot easier, but with pure javascript you can do it.
In the CSS you'll need to use transitions
#thing { position:relative;
top: 0px;
opacity: 0.8;
-moz-transition: top 1s linear, opacity 1s linear;
-webkit-transition: top 1s linear, opacity 1s linear;
}
then in the javascript when you change the position of the element, it should change via the css transitions.
var toggleUpdatesPulldown = function(event, element, user_id) {
if( element.className=='updates_pulldown' ) {
element.style.top = someValue; //something like '100px' will slide it down 100px
element.style.opacity = '1.0'; //will fade the content in from 0.8 opacity to 1.0
element.className= 'updates_pulldown_active';
showNotifications();
EDIT - provided jQuery code
call the jQuery library, most easily done from the google hosting
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
make the hover function
$(document).ready(function() {
$('.updates_pulldown').hover( //first function is mouseon, second is mouseout
function() {
$(this).animate({top: '50px'}).animate({opacity: '1.0'});
},
function() { //delay 1000 milliseconds, animate both position and opacity
$(this).delay(1000).animate({top: '0px'}).animate({opacity: '0.5'});
}
)
})
the function timing will be the same as whatever you set it to in the css with transition tags. using 'this' instead of the class name again makes sure that the effect only occurs on the specific instance of the class that is hovered over. im not sure if this animation is exactly what you were asking for, but if i understand the question correctly then the main functionality will work for you. just change the numbers and such to fit your needs.

Categories