Skip to content Skip to sidebar Skip to footer

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 o

Solution 1:

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')

Post a Comment for "Javascript Or Jquery Get Background-position Properties Of A Given Element And Then +/- To That Dynamically"