带有函数和属性的jQuery CSS

时间:2013-09-20 21:38:11

标签: jquery css properties

说我有这样的事情:

$('#someID').css({
    left: function(index,value) { return newv(value, 37, 39); },
    top: function(index,value) { return newv(value, 38, 40); }
});

我希望包含transform:rotate(30deg)属性。我该怎么做呢?经过一番搜索,我没有找到任何东西。

添加几个没有函数的属性:

$('#someID').css({
   "transform": "rotate(30deg)",
   "top": "-60px",
   "left": "600px"        
});

添加单个属性:

$('#someID').css("transform", "rotate(30deg)");

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

你可以尝试:

$('#someID').css({
    left: function(index,value) { return newv(value, 37, 39); },
    top: function(index,value) { return newv(value, 38, 40); },
    "transform": "rotate(30deg)"
});

答案 1 :(得分:0)

我不确定你要求的是什么,但是:

$('#someID').css({
    left: function(index,value) { return newv(value, 37, 39); },
    top: function(index,value) { return newv(value, 38, 40); },
    transform: 'rotate(30deg)'
});

工作正常

你也可以在这里使用一个功能:

$('#someID').css({
    left: function(index,value) { return newv(value, 37, 39); },
    top: function(index,value) { return newv(value, 38, 40); },
    transform: function(index,value) { return "rotate(" + Math.random() * 90 + "deg)" }
});

但是获取之前的值很棘手,因为它甚至不再是字符串,而是变换矩阵。所以最好的方法就是将之前的值保留在其他地方并使用它。例如:

$('#someID').css({
    left: function(index,value) { return newv(value, 37, 39); },
    top: function(index,value) { return newv(value, 38, 40); },
    transform: function(index,value) {
        value = this.rotate || 0;
        return "rotate(" + (this.rotate = newv(value, 39, 41)) + "deg)" }
});