Javascript/jQuery:动画发生两次
Javascript/jQuery: animation happening twice?
我正在编写一个简单的函数,向它传递一个jQuery对象,然后它应该变成红色,并逐渐变回原来的颜色。
这是:
function flash_cell(target) {
var original_background_color = target.css('background-color');
target.css("background-color", "#d79795");
target.animate({ "background-color": original_background_color}, 1500);
}
相当简单。还是?因为当我运行这个函数时,它会做它应该做的事……但后来它又变红了。为什么?
如果,您的脚本应该可以工作
- 您正在使用文档就绪
$(function(){ /*Code here*/ }); // Dom is ready
- 使用jQuery UI(否则将不会发生背景动画)
- 您正在向fn传递一个jQuery对象元素参数
flash_cell( $("#el") )
$(function(){
function flash_cell(target) {
var original_background_color = target.css('background-color');
target.css("background-color", "#d79795");
target.animate({ "background-color": original_background_color}, 1500);
}
// ___________________________
var $div = $('div');
flash_cell($div); // Do it when DOM is ready
$div.click(function(){
flash_cell( $(this) ); // Do it on click
});
// No other flashes will occur (unless you click it ;) ).
});
演示
关于元素再次变红。。。可能你在不同的地方调用你的函数,从你发布的代码中很难说。详细探索,你会发现你的错误。
相关文章:
- Jquery点击事件必须点击两次
- 防止双击执行两次jQuery post请求
- 单击jQuery会激发两次
- 为什么jQuery下拉列表需要单击两次
- 我的jQuery加载请求是否被调用了两次
- $().单击需要按两次.Jquery JSP
- JQuery脚本没有'Don’我不能工作两次
- jquery on触发器按键事件两次
- jQuery 方法调用了两次
- Rails-jQuery 为什么我必须单击两次才能显示 jQuery 效果
- 为什么要声明两次 jQuery
- jQuery切换了Firefox中所需的两次点击
- 如何使用 jQuery 交换网页中的两个图像(连续两次点击)
- jQuery 每个循环返回数据两次
- 更改jquery需要点击两次
- 发送前jQuery!确认警报两次
- JQuery定位:我可以滚动到正确的位置一次,但不能滚动两次
- 我必须点击两次才能使用jQuery激活函数
- Parameters .replace()两次jQuery插件不起作用
- 点击复选框时触发两次jQuery点击事件