开发者

jQuery - Setting background position from variables

开发者 https://www.devze.com 2023-02-08 01:50 出处:网络
Im getting the X and Y co-ordinates or a selected area, Im then using that to set the position of an image in another DIV.

Im getting the X and Y co-ordinates or a selected area, Im then using that to set the position of an image in another DIV.

Vars:

selection.x1 (This will need to be a negative value)
selection.y2

I need to write using Javascript or jQuery the following:

div.css('background-position', -selection.xl selection.y2);

But this just give me an error, how can I add two background postion values to the CSS开发者_运维知识库?


var x1 = '-100';
var y2 = '100';

$('#divID').css({
  'background-position':x1+'px '+y2+'px'
});

That works for me, just ensure you have a background-position in your css as well (even if it's set to 0px 0px

Edit for @logic-unit
$('#divID').css('background-position', x1+'px '+y2+'px');

0

精彩评论

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

关注公众号