开发者

Using Javascript to change the background-position CSS - small problem

开发者 https://www.devze.com 2023-03-02 21:46 出处:网络
Probably an easy question here (I hope) but my brain\'s just stopped working. I have this javascript: value.obj.css(\'background-position\', value.xStart + 10)

Probably an easy question here (I hope) but my brain's just stopped working.

I have this javascript:

       value.obj.css('background-position', value.xStart + 10)

which changes the background-position of my object by 10 in the x direction.

However I also want to be able to change the position in the y direction 开发者_Python百科but in a different amount (ie, 40). the code being "value.yStart + 40"

Background-position in css is written like "background-position: 30 30;". When I use the above javascript to change it, it comes out "background-position: 40 50%;" but I want it to be "background-position: 40 70;" Please help... been a long day :)


I would use:

value.obj.css({ 'background-position': (value.xStart + 10) + 'px ' + (value.yStart + 40) + 'px' });


Well dont tell people how background-position works, because you got it a little wrong.

Either you use % or px: so:

 background-position: 30px 30px;
 //or:
 background-position: 30px 30%;
 //or even:
 background-position: 30% 30%;


Actually, it must be backgroundPosition (all css properties are camel cased in jquery), and in your case it should be:

.css('backgroundPosition', '30px 30px')

or

.css({ 'backgroundPosition': '30px 30px' })

0

精彩评论

暂无评论...
验证码 换一张
取 消