【问题标题】:Making a switch toggle darker using CSS使用 CSS 使开关切换更暗
【发布时间】:2015-12-06 19:20:47
【问题描述】:

我有这个基本的开关切换脚本,我在网上找到并实施到我的项目中。我的网站背景似乎有点太亮了,按钮不能很好地坐下来,而且很难看到,所以我的问题是,我如何编辑这个开关切换 CSS 代码以使其更暗一点,以便我可以使用它在我的网站上?

我只是真的担心会使实际的切换按钮变暗 - 这是最糟糕的部分,我尝试摆弄脚本但似乎找不到任何改变切换的部分?

这是我的JSfiddle

.container {
  margin: 50px auto;
  width: 280px;
  text-align: center;
}
.container > .switch {
  display: block;
  margin: 12px auto;
}

.switch {
  position: relative;
  display: inline-block;
  vertical-align: top;
  width: 56px;
  height: 20px;
  padding: 3px;
  background-color: #03C;
  border-radius: 18px;
  box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  background-image: -webkit-linear-gradient(top, #eeeeee, white 25px);
  background-image: -moz-linear-gradient(top, #eeeeee, white 25px);
  background-image: -o-linear-gradient(top, #eeeeee, white 25px);
  background-image: linear-gradient(to bottom, #eeeeee, white 25px);
}

.switch-input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}

.switch-label {
  position: relative;
  display: block;
  height: inherit;
  font-size: 10px;
  text-transform: uppercase;
  background: #eceeef;
  border-radius: inherit;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
  -webkit-transition: 0.15s ease-out;
  -moz-transition: 0.15s ease-out;
  -o-transition: 0.15s ease-out;
  transition: 0.15s ease-out;
  -webkit-transition-property: opacity background;
  -moz-transition-property: opacity background;
  -o-transition-property: opacity background;
  transition-property: opacity background;
}
.switch-label:before, .switch-label:after {
  position: absolute;
  top: 50%;
  margin-top: -.5em;
  line-height: 1;
  -webkit-transition: inherit;
  -moz-transition: inherit;
  -o-transition: inherit;
  transition: inherit;
}
.switch-label:before {
  content: attr(data-off);
  right: 11px;
  color: #aaa;
  text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.switch-label:after {
  content: attr(data-on);
  left: 11px;
  color: white;
  text-shadow: 0 1px rgba(0, 0, 0, 0.2);
  opacity: 0;
}
.switch-input:checked ~ .switch-label {
  background: #47a8d8;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.switch-input:checked ~ .switch-label:before {
  opacity: 0;
}
.switch-input:checked ~ .switch-label:after {
  opacity: 1;
}

.switch-handle {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 18px;
  height: 18px;
  background: white;
  border-radius: 10px;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
  background-image: -webkit-linear-gradient(top, white 40%, #f0f0f0);
  background-image: -moz-linear-gradient(top, white 40%, #f0f0f0);
  background-image: -o-linear-gradient(top, white 40%, #f0f0f0);
  background-image: linear-gradient(to bottom, white 40%, #f0f0f0);
  -webkit-transition: left 0.15s ease-out;
  -moz-transition: left 0.15s ease-out;
  -o-transition: left 0.15s ease-out;
  transition: left 0.15s ease-out;
}
.switch-handle:before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -6px 0 0 -6px;
  width: 12px;
  height: 12px;
  background: #f9f9f9;
  border-radius: 6px;
  box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
  background-image: -webkit-linear-gradient(top, #eeeeee, white);
  background-image: -moz-linear-gradient(top, #eeeeee, white);
  background-image: -o-linear-gradient(top, #eeeeee, white);
  background-image: linear-gradient(to bottom, #eeeeee, white);
}
.switch-input:checked ~ .switch-handle {
  left: 40px;
  box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}

.switch-green > .switch-input:checked ~ .switch-label {
  background: #4fb845;
}

【问题讨论】:

    标签: html css button switch-statement toggle


    【解决方案1】:

    你需要改backgroud:#yourcolorcode的class.switch-label

    .switch-label {
      position: relative;
      display: block;
      height: inherit;
      font-size: 10px;
      text-transform: uppercase;
      background: #000;
      border-radius: inherit;
      box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
      -webkit-transition: 0.15s ease-out;
      -moz-transition: 0.15s ease-out;
      -o-transition: 0.15s ease-out;
      transition: 0.15s ease-out;
      -webkit-transition-property: opacity background;
      -moz-transition-property: opacity background;
      -o-transition-property: opacity background;
      transition-property: opacity background;
    }
    

    此外,如果您想更改单选按钮圆圈的颜色,请更新 .switch-handle.switch-handle:before

    .switch-handle {
      position: absolute;
      top: 4px;
      left: 4px;
      width: 18px;
      height: 18px;
      background: #000;
      border-radius: 10px;
      box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
      background-image: -webkit-linear-gradient(top, #000 40%, #000);
      background-image: -moz-linear-gradient(top, #000 40%, #000);
      background-image: -o-linear-gradient(top, #000 40%, #000);
      background-image: linear-gradient(to bottom, #000 40%, #000);
      -webkit-transition: left 0.15s ease-out;
      -moz-transition: left 0.15s ease-out;
      -o-transition: left 0.15s ease-out;
      transition: left 0.15s ease-out;
    }
    .switch-handle:before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -6px 0 0 -6px;
      width: 12px;
      height: 12px;
      background: #000;
      border-radius: 6px;
      box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
      background-image: -webkit-linear-gradient(top, #eeeeee, #000);
      background-image: -moz-linear-gradient(top, #eeeeee, #000);
      background-image: -o-linear-gradient(top, #eeeeee, #000);
      background-image: linear-gradient(to bottom, #eeeeee, #000);
    }
    

    我希望这是你想要的https://jsfiddle.net/mam1yj1f/8/

    【讨论】:

    • 嘿,感谢您的回答 - 抱歉,我的问题不太清楚,我希望弄清楚如何更改左右翻转的白色按钮的颜色。实际的拨动开关。一旦我可以将其变暗,我还可以使用您上面的答案将开关的其余部分变暗以匹配。
    • 为此您需要更改 .switch-handle 和 .switch-handle 的渐变:在课前检查jsfiddle.net/mam1yj1f/5
    • 那是正确的链接吗?好像没什么变化?
    • 这就是我要找的东西!!!您能否更新您的答案以向我展示需要编辑的部分以及如何更改颜色,我将标记为正确!非常感谢!
    【解决方案2】:

    添加

    .switch {
    position: relative;
    display: inline-block;
    vertical-align: top;
    width: 56px;
    height: 20px;
    padding: 3px;
    background-color: #03C;
    border-radius: 18px;
    cursor: pointer;
    background-color: black;
    }
    

    到您的 CSS 样式,jsfiddle

    【讨论】:

    • 感谢您的回答,但我也希望更新白色圆形切换开关?有什么想法可以做到吗?
    • 更新了我的答案,你可以通过改变 .switch 类的背景颜色来改变圆角颜色,祝你好运:)
    【解决方案3】:

    只需改变开关标签颜色如下:

    .switch-label {
      position: relative;
      display: block;
      height: inherit;
      font-size: 10px;
      text-transform: uppercase;
      background: magenta;
      ....
    }
    

    P.D.我使用洋红色只是为了制作示例

    【讨论】:

    • 这会使实际切换本身变暗还是仅使开/关背景颜色变暗?
    【解决方案4】:

    您想要编辑 .switch-handle 和 .switch-handle:before。

    这会使它变暗,您只需调整背景颜色即可获得所需的颜色。

    .switch-handle::before {
    
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -6px 0px 0px -6px;
    width: 12px;
    height: 12px;
    /* change these!*/background: #F9F9F9 linear-gradient(to bottom, #A8A8A8, #959595) repeat scroll 0% 0%;
    border-radius: 6px;
    box-shadow: 0px 1px rgba(0, 0, 0, 0.02) inset;
    
    }
    
    .switch-handle {
    position: absolute;
    top: 4px;
    left: 4px;
    width: 18px;
    height: 18px;
    /* change these!*/background: #FFF linear-gradient(to bottom, #CCC 40%, #F0F0F0) repeat scroll 0% 0%;
    border-radius: 10px;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    transition: left 0.15s ease-out 0s;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2020-10-26
      • 2021-02-13
      相关资源
      最近更新 更多