最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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 es 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

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 es 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

Share Improve this question asked Apr 4, 2012 at 12:40 chrischris 37k53 gold badges147 silver badges256 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论