2

我想知道是否可以animate根据某些标准在 jQuery 中设置一个属性。例子:

var condition = offL > ((wW / 2) - $this.width());
tooltip.css(function() {
  if (condition) {
    return '"right", "auto"';
  } else {
    return '"left", "auto"';
  }
}).animate({
  "top": $this.offset().top + (posT / 2) - (tooltip.height() / 2),
  if (condition) {
    return '"left":"offL - tooltip.width() - 30"';
  } else {
    return '"right", "offR - tooltip.width() - 30"';
  }
}, 300).fadeTo(200, 1);

tooltip和其他变量在前面定义。)

这显然是行不通的。但是,如果他们需要在函数中使用条件参数,该怎么办?

另一个问题,我对我的条件的使用var是否正确?

4

1 回答 1

3

只是在动画之前做出了决定。例如:

var properties = {}
if(condtion) {
  properties = {left: someThing};
else
  properties = {prop1: value1, prop2: value2};
$(obj).animate(properties,'fast');

是的,您对条件的使用是正确的。

var condition = offL > ((wW / 2) - $this.width());

将被评估为真或假,因为它的条件;)

于 2013-01-13T09:14:46.577 回答