css - 使用 css 自定义单选按钮

标签 css radio-button

我一直在尝试自定义默认单选按钮,使其看起来像开/关按钮。
我已经使用 2 个图像(使用 javascript)为开和关状态创建了一个,但是没有用于在两种状态之间转换的动画。

有没有办法使用纯 css(+ 动画)来实现这一点?

我需要这样的按钮-
enter image description here

最佳答案

给你,你也可以用复选框来做到这一点

#mc, #mc2, #mc3 {
  display: none;
}
.switch {
  background: gray;
  width: 40px;
  height: 20px;
  border-radius: 10px;
  display: block;
  position: relative;
  margin: 10px;
}
.switch:hover {
  cursor: pointer
}
.switch:after {
  content: "";
  height: 18px;
  width: 18px;
  border-radius: 9px;
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  background: lightblue;
  transition: ease-out 0.5s;
}
input[type=radio]:checked + label:after,
input[type=checkbox]:checked + label:after{
  left: calc(100% - 19px);
  background: lightgreen;
  }
<p>Radio</p>
<input type="radio" id="mc" name="Switch" />
<label class="switch" for="mc"></label>

<input type="radio" id="mc2" name="Switch" />
<label class="switch" for="mc2"></label>
<p>Checkbox</p>
<input type="checkbox" id="mc3" name="Switch" />
<label class="switch" for="mc3"></label>

关于css - 使用 css 自定义单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39517696/

相关文章:

html - IE 中的 CSS 在服务器上不起作用 - 但在 Localhost 上起作用?

html - 尺寸不适用于移动设备中的 SELECT OPTION

JQuery UI 对象按钮栏的工作方式类似于单选按钮

javascript - 仅根据单选按钮和复选框按钮显示/隐藏 div

java - 如何正确使用单选按钮?

css - 发布带有标签的对齐单选按钮

html - 为较小的屏幕在 div 之间创建空间

javascript - 我将如何在 jQuery 中重复或循环 img 选择器?

jquery - 如何在 jQuery 中选择 ul li

javascript - 单选按钮行 JavaScript