jQuery - 从变量设置背景位置

CLi*_*own 0 javascript jquery

我得到X和Y坐标或选定的区域,我然后用它来设置另一个DIV中的图像的位置.

瓦尔:

selection.x1 (This will need to be a negative value)
selection.y2
Run Code Online (Sandbox Code Playgroud)

我需要使用Javascript或jQuery编写以下代码:

div.css('background-position', -selection.xl selection.y2);
Run Code Online (Sandbox Code Playgroud)

但这只是给我一个错误,我怎样才能在CSS中添加两个背景位置值?

Bar*_*der 10

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

$('#divID').css({
  'background-position':x1+'px '+y2+'px'
});
Run Code Online (Sandbox Code Playgroud)

这对我有用,只要确保你background-position的css中也有一个(即使它被设置为0px 0px

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