2013-04-16 49 views
0

对于JavaScript“类”,我有几个几乎相同的CSS动画函数。我在下面发布的是高度,但我也有一些宽度,左侧等。对于我来说,对于基本相同的任务有不同的功能似乎相当多余,我可以用下面的函数替换高度什么样的CSS属性参数?将CSS属性作为参数传递给JavaScript函数

WindowItem.prototype.animateHeight = function(inElement, inTarget) { 
    var selfCall = true; 
    var currHeight = parseInt(inElement.style.height, 10); 

    if (this.isExpanded) { 
     if (currHeight < inTarget) { 
      currHeight += ((inTarget-currHeight)>>3)+1; 
      if (currHeight >= inTarget) { 
       currHeight = inTarget; 
       selfCall = false; 
      } 
     } 
    } 
    else { 
     if (currHeight > inTarget) { 
      currHeight -= ((currHeight-inTarget)>>3)+1; 
      if (currHeight <= inTarget) { 
       currHeight = inTarget; 
       selfCall = false; 
      } 
     } 
    } 
    inElement.style.height = currHeight+"px"; 

    if (selfCall) { 
     var self = this; 
     setTimeout(function() { 
      self.animateHeight(inElement, inTarget); 
     }, 33); 
    } 
} 

编辑:也许我应该也已经发布了,我如何调用这个,我不知道什么被路过的功能在这个例子中,指定高度:this.animateHeight(this.imgWindow, 0);

+2

'cssProperty = “高度”'和'inElement.style [cssProperty]'? – user2264587

+0

我刚刚编辑了我的帖子,我不太明白如何处理你的代码,请你解释一下吗? – asimes

+3

如果你想使用这个函数给定任何属性,你可以使用参数this.animateProperty(this.imgWindow,0,“height”);',其中'WindowItem.prototype.animateHeight = function(inElement,inTarget ,cssProperty)......“这就是我的意思。除非我误解了这个问题 – user2264587

回答

1

正如我在写我的评论:

,如果你想使用这一功能对于任何财产,你可以使用参数

this.animateProperty(this.imgWindow, 0, "height"); 

其中

WindowItem.prototype.animateHeight = function(inElement, inTarget, cssProperty) 
... 

和代替

inElement.style.height 

使用

inElement.style[cssProperty] 
相关问题