Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the target css property value during a css3 transition in Javascript?

I've two divs positioned absolutly and I position them relative to each other. When width of one is change, then i recalculate and set positions. While I use css3 transition on 'width' property, when i try to get 'width' of animated one, it gives me the current value on dom. But i want to get the target value of transition to set positions correctly on begin of transition effect. Is this possible to get the target value via javascript or other ways?

EDIT

Below is a jsfiddle demonstrates my issue. It alerts '100px' but I need to get '300px'.

http://jsfiddle.net/Mdbgs/

Thanks.

like image 317
Halil Ibrahim Avatar asked Aug 28 '13 11:08

Halil Ibrahim


People also ask

When working with the transition-property How can you target all available CSS properties for the transition effect?

Transitioning two or more properties You can transition two (or more) CSS properties by separating them with a comma in your transition or transition-property property. You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.

Are transition properties inherited?

Specifically around properties, like transition properties, that are not inherited by default. Run this demo in my JavaScript Demos project on GitHub. First off, this is not an AngularJS-specific problem (as you might be lead to believe from the post title). This is just a byproduct of CSS behavior.

Which of the following is a property of transition in CSS?

The transition-delay property specifies a delay (in seconds) for the transition effect.

Which of the following properties Cannot be used with transition?

It does not show fixed valency.


2 Answers

That's because .css('width') is calling getComputedStyle on the element, which does return the transitioning value. If you did directly access the style, you would get what you just had set:

document.getElementById('transition_div').style.width
$('#transition_div').prop('style').width
$('#transition_div')[0].style.width

(updated fiddle)

like image 190
Bergi Avatar answered Oct 19 '22 07:10

Bergi


You could use the transitionend event: (see for equivalent prefixed vendor)

$('#transition_div').css('width', 300).on("transitionend", function () {
    alert($(this).css('width'));
});

DEMO

like image 38
A. Wolff Avatar answered Oct 19 '22 09:10

A. Wolff