Case
Case

Reputation: 4272

Jquery backgroundPosition Animation Question

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?

Upvotes: 3

Views: 1779

Answers (4)

Case
Case

Reputation: 4272

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.

Upvotes: -1

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827416

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];

Upvotes: 2

o.k.w
o.k.w

Reputation: 25810

Have you tried this?

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

It should return something like "nnpx yypx".

Upvotes: 0

Chad
Chad

Reputation: 3227

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.

Upvotes: 1

Related Questions