Erik Moldtmann Erik Moldtmann - 5 months ago 16
Javascript Question

JS get element position after animation interrupt

I have an element, which I move across the screen using an animation:

slide.animate({'left': value*100 + '%'}, transitiontime * 1000)


When a specific event occurs, I stop the animation using

slide.stop();


now I want to get the current 'left' value using

slide.css('left');


but unfortunately I get a 'px' value instead of percent, which I used in the animation.
Is there a way to get the percent value?

Answer

this happens because css() actually uses javascript's getcomputedstyle() function and it always returns in pixels.

you can achieve what you want like this:

var left=(100 / $(window).width()) * parseInt(slide.css('left').split('px')[0]) + '%';

the above code returns a value in percentage DEMO

Comments