CSS3过渡如何,为无线电选择按钮

时间:2016-05-18 12:18:04

标签: html css css3

我有一个收音机选择按钮,我希望使用CSS3的幻灯片效果转换,而不仅仅是我目前使用CSS3的淡入/淡出效果。

可以用纯CSS3完成吗?我不想使用任何jquery,因为我希望它尽可能保持简洁。

谢谢。



body {
  font: 16px Arial, sans-serif;
  background-color: #e0e0e0;
}
/* Hiding Radio */

input[type="radio"] {
  display: none;
}
/* switch-bar */

.switch-canvas {
  width: 200px;
  margin: 50px auto;
  height: 40px;
  padding: 5px;
  background-color: #000;
  font-size: 0;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
label {
  display: inline-block;
  font-size: 16px;
  font-weight: bold;
  width: 50%;
  height: 40px;
  color: #222;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  -webkit-transition: all 0.3s;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
input[type="radio"]:checked + label {
  background-color: #fff;
  color: green;
  border-radius: 2px;
}

<div class="switch-canvas">
  <input type="radio" id="female" name="sex" checked="" />
  <label for="female">Female</label>
  <input type="radio" id="male" name="sex" />
  <label for="male">Male</label>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

我在单选按钮后添加了跨度,因此他将是标记所选项的白色圆角方块。

body {
  font: 16px Arial, sans-serif;
  background-color: #e0e0e0;
}
/* Hiding Radio */

input[type="radio"] {
  display: none;
}
/* switch-bar */

.switch-canvas {
  width: 200px;
  margin: 50px auto;
  height: 40px;
  padding: 5px;
  background-color: #000;
  /*font-size: 0;*/
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  position:relative;
}
label {
  position: relative;
  z-index:1;
  float:left;
  font-size: 16px;
  font-weight: bold;
  width: 50%;
  height: 40px;
  color: #222;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
}
/*label {
display: inline-block;
font-size: 16px;
font-weight: bold;
width: 50%;
height: 40px;
color: #222;
line-height: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
-webkit-transition: all 0.3s;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}*/
input[type="radio"]:checked + label {
  /*background-color: #fff;*/
  color: green;
  /*border-radius: 2px;*/
}

span {
  position:absolute;
  top: 5px;
  left: 5px;
  width: 50%;
  height: 40px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  -webkit-transition: all 0.3s;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  background-color: #fff;
  transform:translate(0);
}

#male:checked ~ span {
  transform:translateX(100%);
  left:-5px;
}
<div class="switch-canvas">
  <input type="radio" id="female" name="sex" checked="" />
  <label for="female">Female</label>
  <input type="radio" id="male" name="sex" />
  <label for="male">Male</label>
  <span></span>
</div>

http://jsbin.com/tazahek/edit?html,css

它不适用于IE8及更早版本(显然)