animate / any函数内的条件语句

问题描述 投票:2回答:1

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

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的使用状况是否正确?

jquery if-statement jquery-animate
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());

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

© www.soinside.com 2019 - 2024. All rights reserved.