jQuery animate 循环卡在第一个循环中
jQuery animate loop stuck in first loop
我正在尝试在jQuery上做一个简单的动画。这个想法是将图像向右移动,然后回到开始的位置。
A 编写了以下代码:
function logo(img) {
$(img).animate( {
left: 100
}, 900, function() {
$(img).animate({
left: 0,
}, 900, logo(img))
})
}
而这个 HTML
<div style="position: relative; width:500px; height: 55px; overflow: visible;">
<img class="pena" src="https://www.google.com.br/images/srpr/logo11w.png" width="50px" style="position: absolute; left: 0px; top: 0;" onclick="logo(this);">
</div>
http://jsfiddle.net/rtfmu5za/1/
但是在动画运行的第一时间,她卡住了一段时间。之后,一切正常。
我错了什么?
你的代码确实有一个奇怪的问题,我无法真正解释为什么会发生这种情况。试试这个解决方案,对我有用:
var position = 0;
function logo(img) {
position = position == 0 ? 100 : 0;
$(img).animate( {
left: position
}, 900, function () { logo(this) });
}
http://jsfiddle.net/oqLakcbj/
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- $.每个jquery循环打印一个“;未定义的“;对于getJSON请求后的每个元素,网格数据都会完美地打印出来
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- Javascript 函数在 PHP 中的第一个 while 循环后停止
- V8 GC何时触发其第一个小GC循环(清除)
- jQuery只从循环中获取第一个id值
- 渐明,淡出DIV上循环工作良好-但不显示第一个DIV
- Javascript只从foreach循环中获取第一个id.为什么
- 如何从循环中选择第一个
- 为什么在jQuery中变量只传递for循环的第一个值?