tags:

views:

394

answers:

4

I am using Jquery to animate an object while the user presses a key.

Upon Keyup the object stops moving, but I need to know what the background position of the object is when it stops. Using .stop(true);

Without getting to much into it, I need to be able to get the position of the background. Is there anyway with Javascript or jquery to obtain those numbers?

+1  A: 

You should be able to identify the specific element that has the background image. Then you can use .offset - http://docs.jquery.com/CSS/offset - to get the left/top absolute position of the element with respect to the page.

Chad
A: 

Have you tried this?

$('#yourOject').css('backgroundPosition')

It should return something like "nnpx yypx".

o.k.w
+1  A: 

If you are changing the background position coordinates, you will be able to get the computed style of the element, at the moment you stop the animation simply by using the css method.

It will return you a pair of pixel values separated by a space, something like "Xpx Ypx", you can split that values and use them separately:

var position = $(selector).css('backgroundPosition').split(' '),// ["0px", "0px"]
x = position[0], y = position[1];
CMS
what kind of variable is x? I tried doing x + someInt and it ended not adding integers, but seems like it was concatenating strings.
Hristo
A: 

var position = $(selector).css('backgroundPosition').split(' '),// ["0px", "0px"] x = position[0], y = position[1];

This was almost perfect, The only thing I changed was split('px') so that your able to have the raw number when you use it for other calculations.

Iscariot