Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element is being animated CSS3

Is there any way to check if element is being animated?

But being animated not with jquery's animate, but with css3's transition..

The problem I have is... I have this slider, on arrow click I give it

left = left+200

where left is either

element.position().left

or

parseInt(element.css("left"));

(it doesn't really matter, the problem occurs with either)

the element is being animated with

transition: left 400ms ease-in-out;

so, when the user clicks on the arrow once and then again before the animation finishes, left returns value depending on its position(so instead of say.. 400px, it might return 235.47px since it was clicked in the middle of the animation)..

like image 851
fxck Avatar asked Mar 16 '12 12:03

fxck


People also ask

How can you tell if an element is currently being animated?

When animating elements with jQuery, you can detect if the animation is in progress by using $(':animated').

What triggers a CSS animation?

CSS animations enable web elements to transition from one CSS style configuration to another. The browser can start defined animations on load, but event triggered CSS animations rely on adding and removing classes. AMP supports both animation types.

Is there animation in CSS?

CSS animations make it possible to animate transitions from one CSS style configuration to another. Animations consist of two components, a style describing the CSS animation and a set of keyframes that indicate the start and end states of the animation's style, as well as possible intermediate waypoints.

Are CSS animations good?

CSS animations are great for websites that want to add dynamic, engaging content without placing much more weight on the page. Since they don't require extra scripts, CSS animations are unlikely to slow down your pages.


1 Answers

When you change the left property of an element, you can associate a boolean value with it (using data() for instance) and set it to true to indicate a transition has started. Then, you can bind to the transition end event (which varies depending on the browser) and set the boolean value back to false from your handler to indicate the transition has ended.

The end result is something like:

yourElement.on(
    "transitionend MSTransitionEnd webkitTransitionEnd oTransitionEnd",
    function() {
        $(this).data("transitioning", false);  // Transition has ended.
    }
);

(Note the code above only has to run once.)


if (!yourElement.data("transitioning")) {
    // No transition active, compute new position.
    var theNewLeft = yourElement.position().left + 200;
    // Set new position, which will start a new transition.
    yourElement.data("transitioning", true).css("left", theNewLeft);
}
like image 59
Frédéric Hamidi Avatar answered Oct 18 '22 21:10

Frédéric Hamidi