jQuery变量和执行延迟

时间:2012-07-23 16:59:54

标签: jquery

首先,对于令人困惑的标题感到抱歉。我在jQuery中遇到了一个愚蠢的问题。这是我的代码:

$j('.parent').hover(function(){
     var newWidth = $j(this).find('img').length * 70; // I need number of images within parent and multiply it by 70
     $j(this).stop().animate({'width':newWidth},700); // <-- This does not work
}, function(){
     $j(this).stop().animate({'width':'70'},700); // Back to original width
});

但那里没有动画。如果我提醒newWidth,它给了我正确的答案。我怀疑动画是在将值分配给newWidth时同时执行的,因此没有任何反应。我是否需要在分配变量和执行动画之间添加延迟。还有另一种有效的方法吗?

更新:.parent的css是内嵌,它跟随:

element.style {
background-color: #000000;
border-color: #CCCCCC;
border-radius: 0 0 0 0;
border-style: solid;
border-width: 0;
clear: both;
height: 75px;
overflow: hidden;
padding: 0;
position: relative;
width: 70px;
}

1 个答案:

答案 0 :(得分:0)

您想要更改哪个宽度? .parent宽度或.parent img宽度?因为您更改了this元素的宽度,即.parent。因此它不会为图像设置动画。

您的代码运行正常:http://jsfiddle.net/WouterJ/QvmBa/