javascript - jQuery toggle class animation - Stack Overflow

I found this code:$('li input:checked').click(function() {$(this).parent().parent().toggleCla

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. I want it animated when it has the class, and when not, not only when clicked for the first time.

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. I want it animated when it has the class, and when not, not only when clicked for the first time.

Share edited Aug 29, 2013 at 10:55 Ozzy 8,3227 gold badges57 silver badges96 bronze badges asked Aug 14, 2013 at 15:08 Amar SylaAmar Syla 3,6633 gold badges32 silver badges71 bronze badges 2
  • Please provide the css of the class you toggle. – benestar Commented Aug 14, 2013 at 15:12
  • Just a background color, nothing else. – Amar Syla Commented Aug 14, 2013 at 15:28
Add a ment  | 

3 Answers 3

Reset to default 1

This is easy with CSS transitions:

JS:

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

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:

CSS:

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

OR for all <INPUT> elements following an <LI><INPUT> bination:

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

You get the idea of it..

The jquery.toggleClass function isn't made for fading anyhow. See http://api.jquery./toggleClass/ for more details.

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/en-US/docs/Web/Guide/CSS/Using_CSS_transitions.

Maybe because you are using "input:checked"

try using

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

this is working for me.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745070278a4609502.html

相关推荐

  • javascript - jQuery toggle class animation - Stack Overflow

    I found this code:$('li input:checked').click(function() {$(this).parent().parent().toggleCla

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信