Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript or jquery get background-position properties of a given element and then +/- to that dynamically

I have a handful of elements I want to set roll overs on, how ever I am looking to do the calculating on the fly rather than manually setting the position each time for each roll over as over time there will be many. Currently I am using a sprite, that as the time comes I append more to it.. Always side by side, and always 10px padding all around for the per image aspect of it.

So I know if my defualt position is always 0 100 for example and I want to get the roll over for that then I know its going to be 50 100 How can I go about just having a method of taking the 2 values of that but only adding to one of them

like image 954
chris Avatar asked Apr 04 '12 12:04

chris


1 Answers

You need to grab the background-position and break it into two values, perform the calculation, that set the values back as a string.

For example

myPos = $('.selection').css("background-position").split(" ")

myPos[0] <-- will contain the X-value, "50px"
myPos[1] <-- will contain the Y-value, "100px"

You then need to turn them into integers: (assume you're doing the same with myPos[1])

myPos[0] = parseInt(myPos[0].replace("px",""))

Then you do the math and assign them back:

myPos[0] = myPos[0] + 100

$('.selection').css("background-position", myPos[0]+'px ' + myPos[1] + 'px')
like image 156
Diodeus - James MacFarlane Avatar answered Sep 24 '22 19:09

Diodeus - James MacFarlane