【问题标题】:How to animate the width and calculate the new left position at the same time with jQuery?如何使用jQuery同时为宽度设置动画并计算新的左侧位置?
【发布时间】:2011-08-21 14:37:42
【问题描述】:

我对 jQuery 中的 animate() 函数有疑问:

$(this).addClass(activeClass).animate({
  position: 'absolute'
  width: '200%',

//Here is the problem. I need the new outerWidth(true) 
//(Width+Padding+Margin+Border) of the animated element to calculate 
//the new position. I want it to be centered from it's old position. 
//But it is still using the current outerWidth.

  left : -($(this).outerWidth(true)-defaultOuterWidth)/2

});

示例:

假设我有一个 div,我想将其调整为 200%。 div开头的边距为20px,宽度为200px。

应用“activeClass”后,边距现在可能是 40px 宽,现在它有一个边框和一个 400px 的新宽度,因为 animate() 函数是这样说的;)

因此我需要在动画期间使用新的 outerWidth(true) 来计算新的位置。

有什么解决办法吗?

【问题讨论】:

    标签: javascript jquery jquery-animate


    【解决方案1】:

    我认为您可以在动画中使用自定义“步进”函数来为左侧位置进行自己的计算。这里有描述:http://api.jquery.com/animate/

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2014-07-27
      • 2023-03-28
      • 1970-01-01
      • 2014-06-27
      • 2013-11-06
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多