CSS组件之CheckBox自定义样式

本文主要是通过CSS实现如何自定义CheckBox的自定义样式。

在项目的开发过程中难免会经常遇到需要自定义CheckBox的样式问题,原生的CheckBox样式实在是很难符合现代人越来越高的审美观。今天在项目的开发过程中又遇到了这个问题,索性直接将代码记录下来,方便后续使用。

首先是html代码如下:

其次是CSS代码:

/*--相关的单选复选按钮--*/
@keyframes hover-color {       /**复选框或单选框  边框 hover**/
  from {
    border-color: #c0c0c0; }
  to {
    border-color: #29A176; } }

.magic-radio,
.magic-checkbox {           /**必须要把    radio和 checkbox的隐藏或者切割掉**/
  position: absolute;
  display: none; }

.magic-radio[disabled],           /**radio   checkbox   指定disabled   html属性时的鼠标指针**/
.magic-checkbox[disabled] {
  cursor: not-allowed; }

.magic-radio + label,
.magic-checkbox + label {
  position: relative;
  display: block;
  padding-left: 25px;
  cursor: pointer;
  vertical-align: middle;
  }
  .magic-radio + label:hover:before,     /*选矿的 hover边框动画  */
  .magic-checkbox + label:hover:before {
    animation-duration: 0.4s;
    animation-fill-mode: both;
    animation-name: hover-color; }
  .magic-radio + label:before,
  .magic-checkbox + label:before {/*用before来模拟选框的框*/
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    width: 20px;
    height: 20px;
    content: '';
    border: 1px solid #c0c0c0; }
  .magic-radio + label:after,
  .magic-checkbox + label:after {/*用after模拟选框的 对号或园点    默认先隐藏*/
    position: absolute;
    display: none;
    content: '';
    }

.magic-radio[disabled] + label,
.magic-checkbox[disabled] + label {/*选框  被禁用时的外观*/
  cursor: not-allowed;
  color: #e4e4e4;
  }
  .magic-radio[disabled] + label:hover, .magic-radio[disabled] + label:before, .magic-radio[disabled] + label:after,
  .magic-checkbox[disabled] + label:hover,
  .magic-checkbox[disabled] + label:before,
  .magic-checkbox[disabled] + label:after {
    cursor: not-allowed; }/*禁用时的指针*/
  .magic-radio[disabled] + label:hover:before,
  .magic-checkbox[disabled] + label:hover:before {
    border: 1px solid #e4e4e4;
    animation-name: none; }/*禁用时的选框和填充*/
  .magic-radio[disabled] + label:before,
  .magic-checkbox[disabled] + label:before {
    border-color: #e4e4e4; }

.magic-radio:checked + label:before,
.magic-checkbox:checked + label:before {
  animation-name: none; }/*选框被选中时  去掉hover 动画*/

.magic-radio:checked + label:after,
.magic-checkbox:checked + label:after {/*选框被选中时  显示after填充*/
  display: block;
  }

.magic-radio + label:before {/*radio的选框应该是个圆形*/
  border-radius: 50%; }

.magic-radio + label:after {/*模拟出radio的园点填充*/
  top: 7px;
  left: 7px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #29A176; }

.magic-radio:checked + label:before {
  border: 1px solid #29A176; }

.magic-radio:checked[disabled] + label:before {
  border: 1px solid #c9e2f9; }

.magic-radio:checked[disabled] + label:after {
  background: #c9e2f9; }

.magic-checkbox + label:before {
  border-radius: 3px; }

.magic-checkbox + label:after {/*模拟出checkbox选框的对号填充*/
  top: 2px;
  left: 7px;
  box-sizing: border-box;
  width: 6px;
  height: 12px;
  transform: rotate(45deg);
  border-width: 2px;
  border-style: solid;
  border-color: #fff;
  border-top: 0;
  border-left: 0; }

.magic-checkbox:checked + label:before {
  border: #29A176;
  background: #29A176; }

.magic-checkbox:checked[disabled] + label:before {
  border: #c9e2f9;
  background: #c9e2f9; }

 HTML文件的布局必须按照上述文件布局使用。如果希望自定义其他样式,只需要修改自己想要修改的部分内容即可。

 

你可能感兴趣的:(个人笔记)