【问题标题】:Custom checkbox not transitioning自定义复选框未转换
【发布时间】:2018-08-11 00:08:13
【问题描述】:

所以我得到了这个 CSS 自定义复选框,但转换似乎对它不起作用。代码不是我写的,我是从here 得到的。

这是我稍作修改的版本:

.checkbox {
  position: absolute; 
  opacity: 0; 
}

.checkbox + label {
  position: relative;
  cursor: pointer;
  padding: 0;
}

.checkbox + label:before {
  content: '';
  margin-right: 10px;
  display: inline-block;
  vertical-align: text-top;
  width: 20px;
  height: 20px;
  background: #ccf2ff;
	border-radius: 6px;
	box-shadow: 0 0 0 3px #99e6ff;
}

.checkbox:hover + label:before {
  background: #80dfff;
}
  
.checkbox:focus + label:before {
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.12);
}

.checkbox:checked + label:before {
  background: #33ccff;
}
  
.checkbox:disabled + label {
  cursor: auto;
}

.checkbox:disabled + label:before {
  box-shadow: none;
  background: #ddd;
}

.checkbox:checked + label:after {
  content: '';
  position: absolute;
  left: 5px;
  top: 9px;
  background: white;
  width: 2px;
  height: 2px;
  box-shadow: 
    2px 0 0 white,
    4px 0 0 white,
    4px -2px 0 white,
    4px -4px 0 white,
    4px -6px 0 white,
    4px -8px 0 white;
  transform: rotate(45deg);
}

.checkbox, .button, input, select
{
	-webkit-transition-duration: 0.2s; 
	-moz-transition-duration: 0.2s;
	-o-transition-duration: 0.2s;
	transition-duration: 0.2s;
}

其他所有转换都可以正常工作,只是复选框是问题所在。我在这里错过了什么吗?还是导致转换不起作用的代码有问题?请帮帮我。

【问题讨论】:

  • 除非您同时发布 HTML,否则演示将无法运行。

标签: css checkbox css-transitions


【解决方案1】:

您正在将自定义复选框样式应用于label 标记的:before 伪元素,因此您只需要将过渡也应用于该元素:

.checkbox, 
.button, 
input, 
select, 
.checkbox + label:before
{
    -webkit-transition-duration: 0.2s; 
    -moz-transition-duration: 0.2s;
    -o-transition-duration: 0.2s;
    transition-duration: 0.2s;
}

【讨论】:

  • 哦,就是这么简单!谢谢!
猜你喜欢
  • 2014-03-20
  • 2011-09-23
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
相关资源
最近更新 更多