Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect the end of a CSS transition on a 'specific' element while multiple transitions are taking place?

I have been using the following to detect the end of a CSS3 transition, like so:-

    CACHE.previewControlWrap.css({
                'bottom':'-217px'
            }).one('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd', function () {
                CACHE.songWrap.css({
                    'bottom': '0'
                });
     });

This works perfectly, a CSS transition takes place, then when it completes, another takes place.

However when I nest this anonymous function down to a third level, it does not work. The third transition 'end' event is fired at the same time as the second, instead of chaining them one after another (as would happen with jQuery .animate())

What I would like to do is tie the 'transitionend' event to a 'specific' element. Currently it seems to look for a transitionend event on any element and fires accordingly. If not, is there another workaround so that I can have three successive css transition events queued up all firing after the previous one completes.

Thanks in advance.

Below is the code that is causing the issue:-

if(Modernizr.csstransitions){

        CACHE.leftcolbottom.css({
            'left':'-230px'
        });
        CACHE.songwrap.css({
            'left':'100%',
            'right': '-100%'
        });
        CACHE.previewControlWrap.css({
            'bottom':'-217px'
        }).one('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd', function () {
            CACHE.songWrap.css({
                'bottom': '0'
            });
            CACHE.middle.css({
                'bottom': '350px'
            }).one('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd', function () {
                CACHE.slidewrap.css({
                    'left': '50%',
                    'right': '0%'
                });
                CACHE.leftcoltop.css({
                    'left': '0%'
                });     
            });         
        });

    }
like image 893
gordyr Avatar asked May 22 '12 17:05

gordyr


People also ask

What is transitionend event in CSS?

Definition and Usage. The transitionend event occurs when a CSS transition has completed. Note: If the transition is removed before completion, e.g. if the CSS transition-property property is removed, the transitionend event will not fire.

How do you use transitions in CSS?

CSS Transitions. CSS transitions allows you to change property values smoothly, over a given duration. Mouse over the element below to see a CSS transition effect: ... ease-in-out - specifies a transition effect with a slow start and end; cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-bezier function;

How do you fire a transition event in JavaScript?

To this, we'll add some JavaScript to indicate that the transitionstart, transitionrun, transitioncancel and transitionend events fire. In this example, to cancel the transition, stop hovering over the transitioning box before the transition ends. For the transition end event to fire, stay hovered over the transition until the transition ends.

What is the default duration of transition effect in HTML?

Note: If the duration part is not specified, the transition will have no effect, because the default value is 0. The following example shows a 100px * 100px red <div> element. The <div> element has also specified a transition effect for the width property, with a duration of 2 seconds:


1 Answers

Okay, i've actually found the answer myself, hopefully this will help someone else with the same issue.

The solution is to use the standard jQuery .on() method rather than the 'fire once' .one() method. Then check for the target to see if it the element you bound the event to, then finally, remove the event handler within the same anonymous function.

The makes the 'third' nested transition code look like this in my case:-

}).on('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd', function (e) {
                if($(e.target).is(this)){
                    CACHE.slidewrap.css({
                        'left': '50%',
                        'right': '0%'
                    });
                    CACHE.leftcoltop.css({
                        'left': '0%'
                    });
                    $(this).off('webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd');
                }
            });     

If anyone else has a more elegant solution, please let me know and I will award the answer to you as appropriate.

like image 167
gordyr Avatar answered Oct 26 '22 21:10

gordyr