简体   繁体   English

jQuery切换类动画

[英]jQuery toggle class animation

I found this code: 我发现此代码:

$('li input:checked').click(function() {
    $(this).parent().parent().toggleClass("uncheckedBoxBGColor", 1000);
});

It's working well, when a click the element for the first time. 第一次单击该元素时,它运行良好。 It fades the background color, but when I click it again, it delays for 1000 ms, and then flashes the other background color. 它使背景色褪色,但是当我再次单击它时,它会延迟1000毫秒,然后闪烁其他背景色。 I want it animated when it has the class, and when not, not only when clicked for the first time. 我希望它在上课时和不在上课时(不仅是第一次单击时)具有动画效果。

The jquery.toggleClass function isn't made for fading anyhow. 无论如何,jquery.toggleClass函数都不是用于衰落的。 See http://api.jquery.com/toggleClass/ for more details. 有关更多详细信息,请参见http://api.jquery.com/toggleClass/

If you want to fade in/out the background color, try using the CSS3 transition feature like explained at Mozilla's side: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions . 如果您想淡入/淡出背景色,请尝试使用CSS3过渡功能,如Mozilla侧面所述: https : //developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Using_CSS_transitions

This is easy with CSS transitions: 使用CSS过渡很容易:

JS: JS:

$('li input:checked').click(function() {
    $(this).parent().parent().toggleClass("uncheckedBoxBGColor", 1000);
});

CSS: CSS:

.uncheckedBoxBGColor {
  background-color: orange;
  /*other properties*/
  transition: background-color .3s;
}

This will add the effect whenever the class is turned ON, but when it doesn't have that class then there are no transitions defined. 每当打开类时,都会添加效果,但是当没有该类时,则不会定义任何过渡。 So instead, you can turn on this transition for ALL <LI> elements like so: 因此,您可以为所有<LI>元素启用此转换,如下所示:

CSS: CSS:

li { transition: background-color .3s; }

OR for all <INPUT> elements following an <LI><INPUT> combination: 对于<LI><INPUT>组合之后的所有<INPUT>元素,或:

li input { transition: background-color .3s; }

You get the idea of it.. 你有这个主意。

Maybe because you are using " input:checked " 可能是因为您使用的是“ input:checked

try using 尝试使用

     $('li input[type=checkbox]').click(function () {
            $(this).parent().parent().toggleClass("uncheckedBoxBGColor", 1000);
        });

this is working for me. 这为我工作。

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

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