简体   繁体   English

连续的背景色动画请求因jQuery 1.6.1而失败

[英]Successive background-color animation request failing with jQuery 1.6.1

I'm trying to "flash" an input box by changing the background color briefly and then reverting back to the original bg color using jquery to indicate an error and grab the users attention. 我试图通过短暂更改背景颜色来“刷新”输入框,然后使用jquery还原为原始bg颜色以指示错误并吸引用户的注意。

Here's a fiddle to demonstrate what I'm trying to do. 这是用来证明我正在尝试做的事。

I have to use jquery version 1.6.1. 我必须使用jquery版本1.6.1。 In the fiddle demo, it's using 1.6.4 and the color of the input box never changes at all. 在小提琴演示中,它使用的是1.6.4,输入框的颜色完全不变。 Actually, it doesn't work even with 1.11. 实际上,即使使用1.11,它也不起作用。 In my local tests with my code, the input box changes red with the first animation call, but fails to do anything for the second animation call (to revert the bg color back to white). 在使用代码进行的本地测试中,输入框在第一个动画调用时变为红色,但在第二个动画调用中却不执行任何操作(将bg颜色恢复为白色)。 It just stays red. 它只是保持红色。

I'm using very similar code to do the same thing in another site, except using jquery 1.11 and it works fine. 我正在使用非常相似的代码在另一个站点中执行相同的操作,除了使用jquery 1.11之外,而且效果很好。

Is this just a compatibility issue? 这仅仅是兼容性问题吗? Is there some way I can make this work properly with version 1.6.1 ? 有什么办法可以使1.6.1版正常工作?

Here's the code: 这是代码:

function flashInputBox(id) {
    var input = $('#'+id);
    input.focus();
    input.stop(true).animate({'background-color': '#EC8686'}, 350, function() {
        input.stop(true).animate({'background-color': '#FFFFFF'}, 1000);
    });
}

I forgot to mention that I'm using jQuery UI v1.8.18 我忘了提到我正在使用jQuery UI v1.8.18

The problem is properly replicated now in this fiddle (same code, just added jQuery UI 1.8.18). 现在已经在这个小提琴中正确地复制了该问题(相同的代码,只是添加了jQuery UI 1.8.18)。

Colors aren't numeric values, so they can't be animated. 颜色不是数字值,因此无法设置动画。 From the jQuery documentation for .animate , emphasis mine: .animate的jQuery文档中 ,重点是:

All animated properties should be animated to a single numeric value, except as noted below; 除非另有说明,否则所有动画属性都应设置为单个数值。 most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width , height , or left can be animated but background-color cannot be, unless the jQuery.Color() plugin is used ). 大多数非数字属性不能使用基本的jQuery功能进行动画处理(例如,可以对widthheightleft进行动画处理, 而不能对background-color进行动画处理,除非使用jQuery.Color()插件 )。 Property values are treated as a number of pixels unless otherwise specified. 除非另有说明,否则属性值被视为像素数。 The units em and % can be specified where applicable. 可以在适当的地方指定单位em%

If you don't want to (or can't) use the jQuery.Color plugin, you'll need to animate the color "manually", eg by setting an interval and changing the color at each step. 如果您不想(或不能)使用jQuery.Color插件,则需要“手动”对颜色进行动画处理,例如,通过设置间隔并在每个步骤中更改颜色。

Do you need to use jQuery? 您需要使用jQuery吗? If not, this is way easier in CSS using key frames. 如果没有,这在使用关键帧的CSS中更容易。 If it is, skip my CSS explanation. 如果是这样,请跳过我的CSS说明。

CSS 的CSS

This still uses jQuery, but it gives the animation job to CSS, making your code more legible. 它仍然使用jQuery,但是将动画工作交给了CSS,使您的代码更清晰易读。 I set this up in jsFiddle if you want to check it out: jsFiddle Example 如果您想检查一下,可以在jsFiddle中进行设置: jsFiddle示例

First, setup a keyframe: 首先,设置关键帧:

@keyframes pulse{
    from {
        background: #ec8686;    
    }
    to {
        background: #ffffff;
    }
}

@-webkit-keyframes pulse{
    from {
        background: #ffffff;    
    }
    to {
        background: #ec8686;
    }
}

and attach it to your existing input: 并将其附加到您现有的输入中:

#my-input{
    ...
    -webkit-animation: pulse 5s infinite;
    -webkit-animation-play-state: paused;
    ...
}

Then the jQuery becomes a matter of letting the animation play for a few seconds: 然后,jQuery就变成了让动画播放几秒钟的问题:

function doIt() {
    $("#my-input").css("-webkit-animation-play-state", "running");

     setTimeout(function() { 
           $("#my-input").css("-webkit-animation-play-state", "paused");
     }, 5000);
}

Also, you don't even need the jQuery to trigger the animation. 另外,您甚至不需要jQuery来触发动画。 The button click can directly trigger a CSS animation, however I figured you have some sort of code to check what's in the box for accuracy, so that why I kept your old function. 单击按钮可以直接触发CSS动画,但是我认为您有某种代码可以检查框中的内容是否准确,从而可以保留我以前的功能。

Note that this keyframe ends suddenly, so you can totally have a 0% , 50% , 100% keyframe instead. 请注意,此关键帧突然结束,因此您可以完全使用0%50%100%关键帧。

Now for the raw jQuery way: 现在使用原始的jQuery方法:

jQuery jQuery的

For your jQuery, its much easier just to either specify your input directly (aka $("#my-input-name") ), or if its just one input, I got it working just by using the following code instead: 对于您的jQuery而言,直接指定您的输入(也就是$("#my-input-name") ),或者仅输入一个输入,就更容易了,我只需使用以下代码即可:

function doIt() {
    ...
    input.stop().animate({'background-color': '#EC8686'}, 350, function() {
        // just say input here //
        input.animate({'background-color': '#FFFFFF'}, 1000);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM