在H5移动端页面的开发中常用到checkbox和radio的美化,平常使用的各个框架中也有使用,今天我们自己来尝试美化:
1、radio的美化
//html
<input type="radio" name="a" checked>
<input type="radio" name="a">
//css
input[type=radio]{
/*去除浏览器默认样式*/
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/*自定义样式*/
width: 20px;
height: 20px;
border:1px solid red;
border-radius: 20px;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
outline: none;
position: relative;
}
input[type=radio]::after{
content: '';
display: block;
width: 12px;
height: 12px;
background: red;
border-radius: 12px;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
position: absolute;
top: 3px;
left: 3px;
transform: scale(0);
-webkit-transform: scale(0);
-moz-transform: scale(0);
transition: all ease-in-out 300ms;
-webkit-transition : all ease-in-out 300ms;
-moz-transition : all ease-in-out 300ms;
}
input[type=radio]:checked::after{
transform: scale(1);
-webkit-transform: scale(1);
-moz-transform: scale(1);
}
美化结果:

2、checkbox美化
//html
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
//css--11
input[type=checkbox]{
/*去除浏览器默认样式*/
-webkit-appearance:none;
-moz-appearance: none;
appearance: none;
/*自己样式*/
width: 20px;
height: 20px;
border:1px solid red;
outline: none;
background: transparent;
border:1px solid #00BFFF;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
}
input[type=checkbox]::after{
content: '✔';
display: block;
width: 100%;
height: 100%;
background: #00BFFF;
color: #fff;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-align: center;
line-height: 20px;
/*增加动画*/
transition:all ease-in-out 300ms;
-webkit-transition: all ease-in-out 300ms;
-moz-transition: all ease-in-out 300ms;
opacity: 0;
}
input[type=checkbox]:checked::after{
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
opacity: 1;
}
//css--22
input[type=checkbox]{
/*去掉默认样式*/
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
/*定义自己的样式*/
background: #fff;
border:1px solid #00BFFF;
height: 21px;
width: 51px;
border-radius: 20px;
outline: none;
transition: all ease-in-out 300ms;
-webkit-transition: all ease-in-out 300ms;
-moz-transition: all ease-in-out 300ms;
position: relative;
}
input[type=checkbox]::after{
content: 'off';
display: block;
height: 15px;
width: 15px;
border: 1px solid red;
background: #00BFFF;
border-radius: 12px;
position: absolute;
top: 1px;
left: 2px;
-webkit-transition: all ease-in-out 300ms;
-moz-transition: all ease-in-out 300ms;
transition: all ease-in-out 300ms;
}
input[type=checkbox]:checked{
background: #00BFFF;
}
input[type=checkbox]:checked::after{
content: 'on';
left:30px;
background: #fff;
color: #00BFFF;
}
美化结果:


网友评论