简体   繁体   中英

jQuery slide toggle checkbox cant change background colour

I am trying to change the background colour of the toggle to green when it is turned on. I think I have done everything correctly, but the background just stays grey.

What am I doing wrong?

Here is my fiddle

$('.slider-button').toggle(function(){
    $(this).addClass('on').parent().next('input[type="checkbox"]').attr('checked', 'checked');
    $('.slider-frame').addClass('green');
},function(){
    $(this).removeClass('on').parent().next('input[type="checkbox"]').removeAttr('checked');
    $('.slider-frame').removeClass('green');
});

Thanks in advance.

Add the style

#stage .slider-frame.green {
    background-color: #5fca42 !important;
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    background-image: -webkit-linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
}

Demo: Fiddle

If you change the following, its working:

#stage .slider-frame.green {
    background: #5fca42 !important;
}

So basically just background: instead of background-color:

Updated fiddle: http://jsfiddle.net/84Qkz/2/

you need to add this to your css. This will prevent the override you are currently getting

#stage .slider-frame.green {
    background-image:none;
}

This CSS will solve your problem

#stage .slider-frame.green {
    background-color: #5fca42 !important;
    background-image: -moz-linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
    background-image: -o-linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
    background-image: -webkit-linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
    background-image: linear-gradient(bottom, #5fca42 0%, #5fca42 0.27%, #d8d8d8 100%);
    -moz-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
    box-shadow: 0 1px 0 rgba(255,255,255,.4), inset 0 1px 3px rgba(0,0,0,.32), inset 0 0 3px rgba(0,0,0,.12);
z-index:9999999;
}
  1. You are defining the colour of .slider-button as white.
  2. .slider-button is nested inside .slider-frame .
  3. slider-frame is given the additional CSS rule that makes it green
  4. The green isn't showing because the background colour of the element within it is still white.

Solution...

Change

#stage .slider-frame.green {....}

to...

#stage .slider-frame.green .slider-button {....}

So you are now changing the colour of .slider-button , not .slider-frame

Fiddle me this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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