【问题标题】:Bootstrap Checkbox Switch Background color issue during disable禁用期间的引导复选框切换背景颜色问题
【发布时间】:2020-07-13 12:55:04
【问题描述】:

这些引导复选框没有禁用:

这些引导复选框被禁用:

在禁用状态期间,它会更改背景颜色,但我不想更改我想要的背景颜色,因为它处于禁用状态,所以我该怎么做?

我已经尝试覆盖默认类,但没有任何改变。

这是 bootstrap 的默认 CSS

.switch input:disabled + span {
    background-color:#f1f1f1;
    cursor: not-allowed;
}

我想去掉这个背景色!!

【问题讨论】:

  • background-color: transparent;?如果它没有覆盖,则可能是因为引导程序中的 css 稍后加载,然后是您的自定义 CSS。不过,您总是可以使用background-color: transparent !important;
  • 其实我想要的颜色和开关没有禁用一样
  • 或者我希望在禁用状态下具有相同的背景效果
  • 背景色:#fff;
  • 如果我选中的开关颜色是#2196F3,那么在禁用状态我想要相同的颜色

标签: html css angular-ui-bootstrap


【解决方案1】:

现在我得到了答案:

.switch input:checked:disabled + span {
    background-color:#5d9cec;
    cursor: not-allowed;
}
.switch input:not(:checked):disabled + span {
    background-color:#fff;
    cursor: not-allowed;
}

Ty @Lalit 帮助我

【讨论】:

    【解决方案2】:

    请检查答案,这是我使用您问题中提供的引导默认类和 css 所做的,并找出您做错了什么。

    它似乎对我有用

    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input {display:none;}
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    /* Rounded sliders */
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    .switch input:disabled + span {
        background-color:#2196F3;
        cursor: not-allowed;
    opacity: 0.6;
    }
    .switch input:not(:checked):disabled + span {
        background-color:#ccc;
        cursor: not-allowed;
    opacity: 0.6;
    }
    <label class="switch">
      <input type="checkbox" disabled="disabled">
      <span class="slider round"></span>
    </label>
    
    <label class="switch">
      <input type="checkbox" checked disabled="disabled">
      <span class="slider round"></span>
    </label>

    【讨论】:

    • 是否勾选并禁用当时相同的背景颜色?
    • 是的,请看这个,就像它同时被检查和禁用
    • 我想要相同的颜色 #2196F3 处于禁用状态
    • 请立即检查 .switch input:disabled + span { background-color:#2196F3;游标:不允许;不透明度:0.6; }
    • 或者你也可以去掉不透明度来获得相同的颜色
    【解决方案3】:

    2020 年 Bootstrap v4.5.0 更新答案

    /* Enabled */
    .custom-control-input:checked~.custom-control-label::before
    {
      border-color: red !important;
      background-color: blue !important;
    }
    
    /* Disabled */
    .custom-control-input:checked:disabled~.custom-control-label::before
    {
      border-color: yellow !important;
      background-color: green !important;
    }
    

    如果你想用custom-control-input类覆盖所有输入,!important是必需的。如果您只需要覆盖一个,请尝试将.custom-control-input 位更改为#yourinputid 之类的ID

    【讨论】:

      【解决方案4】:

      请向我们提供一些您的 CSS 代码。关于您的问题,bootstrap css 通常以高特异性选择,您可能希望确保选择器的特异性更高。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-05
        • 1970-01-01
        • 2021-11-27
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多