复选框样式化综合





<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Checkbox��ʽ</title>
<style type="text/css" media="screen">
body{color:#444;font-size:1.6em;background:#ccc;}
.container{width:90%;margin:20px 3%;padding:25px;min-height:400px;height:auto;background: #FFF;}
section{float:left;width:30%;margin:20px 20px;}
hr{ clear:both;}
/*** Start by hiding the checkboxes */
input[type=checkbox] {visibility: hidden;}
/** * Create the slider bar */
.checkboxOne { width: 40px;height: 10px;background: #555;margin: 20px 80px;position: relative;border-radius: 3px;}
/** * Create the slider from the label */
.checkboxOne label {display: block;width: 16px;height: 16px;border-radius: 50%;-webkit-transition: all .5s ease;-moz-transition: all .5s ease;-o-transition: all .5s ease;-ms-transition: all .5s ease;transition: all .5s ease;cursor: pointer;position: absolute;top: -3px;left: -3px;background: #ccc;}
/** * Move the slider in the correct position if the checkbox is clicked*/
.checkboxOne input[type=checkbox]:checked + label {left: 27px;}
/** * Checkbox Two */
.checkboxTwo {width: 120px;height: 40px;background: #333;margin: 20px 60px;border-radius: 50px;position: relative;}
/** * Create the line for the circle to move across */
.checkboxTwo:before {content: '';position: absolute;top: 19px;left: 14px;height: 2px;width: 90px;background: #111;}
/** * Create the circle to click */
.checkboxTwo label {display: block;width: 22px;height: 22px;border-radius: 50%;-webkit-transition: all .5s ease;-moz-transition: all .5s ease;-o-transition: all .5s ease;-ms-transition: all .5s ease;transition: all .5s ease;cursor: pointer;position: absolute;top: 9px;z-index: 1;left: 12px;background: #ddd;}
/* * Create the click event for the checkbox*/
.checkboxTwo input[type=checkbox]:checked + label {left: 84px;background: #26ca28;}
/** * Checkbox Three */
.checkboxThree {width: 120px;height: 40px;background: #333;margin: 20px 60px;border-radius: 50px;position: relative;}
/** * Create the text for the On position */
.checkboxThree:before {content: 'On';position: absolute;top: 12px;left: 13px;height: 2px;color: #26ca28;font-size: 16px;}
/** * Create the label for the off position */
.checkboxThree:after { content: 'Off';position: absolute;top: 12px;left: 84px;height: 2px;color: #ddd;font-size: 16px;}
/** * Create the pill to click */
.checkboxThree label {display: block;width: 52px;height: 22px;border-radius: 50px;-webkit-transition: all .5s ease;-moz-transition: all .5s ease;-o-transition: all .5s ease;-ms-transition: all .5s ease;transition: all .5s ease;cursor: pointer;position: absolute;top: 9px;z-index: 1;left: 12px;background: #ddd;}
/** * Create the checkbox event for the label * @type {[type]} */
.checkboxThree input[type=checkbox]:checked + label { left: 60px;background: #26ca28;}
/** * Checkbox Four */
.checkboxFour {width: 40px;height: 40px;background: #ddd;margin: 20px 90px; border-radius: 100%;position: relative;-webkit-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);-moz-box-shadow: 0px 1px 3px rgba(0,0,0,0.5);box-shadow: 0px 1px 3px rgba(0,0,0,0.5);}
/** * Create the checkbox button */
.checkboxFour label {display: block;width: 30px;height: 30px;border-radius: 100px;-webkit-transition: all .5s ease;-moz-transition: all .5s ease;-o-transition: all .5s ease;-ms-transition: all .5s ease;transition: all .5s ease;cursor: pointer;position: absolute;top: 5px;left: 5px;z-index: 1;background: #333;-webkit-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);-moz-box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);box-shadow:inset 0px 1px 3px rgba(0,0,0,0.5);}
/** * Create the checked state */
.checkboxFour input[type=checkbox]:checked + label {background: #26ca28;}
/** * Checkbox Five */
.checkboxFive {width: 25px;margin: 20px 100px;position: relative;}
/** * Create the box for the checkbox */
.checkboxFive label { cursor: pointer;position: absolute;width: 25px;height: 25px;top: 0;left: 0;background: #eee;border:1px solid #ddd;}
/** * Display the tick inside the checkbox */
.checkboxFive label:after {opacity: 0.2;content: '';position: absolute;width: 9px;height: 5px;background: transparent;top: 6px;left: 7px;border: 3px solid #333;border-top: none;border-right: none;-webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); -o-transform: rotate(-45deg); -ms-transform: rotate(-45deg); transform: rotate(-45deg);}
/** * Create the hover event of the tick */
.checkboxFive label:hover::after {opacity: 0.5;}
/** * Create the checkbox state for the tick */
.checkboxFive input[type=checkbox]:checked + label:after {opacity: 1;}
</style>
</head>
<body>
<section class="container">
<section>
  <div class="checkboxOne">
  <input type="checkbox" value="1" id="checkboxOneInput" name="" />
  <label for="checkboxOneInput"></label>
  </div>
</section>
<section>
  <div class="checkboxTwo">
  <input type="checkbox" value="1" id="checkboxTwoInput" name="" />
  <label for="checkboxTwoInput"></label>
  </div>
</section>
<section>
  <div class="checkboxThree">
  <input type="checkbox" value="1" id="checkboxThreeInput" name="" />
  <label for="checkboxThreeInput"></label>
  </div>
</section>
<section>
  <div class="checkboxFour">
  <input type="checkbox" value="1" id="checkboxFourInput" name="" />
  <label for="checkboxFourInput"></label>
  </div>
</section>
<section>
  <div class="checkboxFive">
  <input type="checkbox" value="1" id="checkboxFiveInput" name="" />
  <label for="checkboxFiveInput"></label>
  </div>
</section>
    <div style="clear:both;"></div>
</section>
</body>
</html>

复选框 Checkbox 是 Web 应用常用控件,随处可见,原生的复选框控件一般就像下面这样:

 选中状态  未选状态

这取决于操作系统和浏览器,有些时候,这种样子并不能满足设计要求,这时需要更为精致的复选框样式。以往只有少数浏览器才支持对这类控件应用样式,比如拿到这样一张设计图:

在过去,想要通过简单地修改样式达成这种设计效果根本不行,不过,现在借助强大的 CSS3 属性 appearance 可以对该类控件有空前的样式控制能力:

input[type="checkbox"] {
  -webkit-appearance: none;
}

这样设置该属性值为 none 就去掉了复选框原有的呈现方式,变成了一个普普通通的元素,然后就可以为之应用样式了,添加如下样式:


input[type="checkbox"] {
  -webkit-appearance: none;
  background: #fff url(i/blue.png);
  height: 22px;
  vertical-align: middle;
  width: 22px;
}

通过结合使用状态伪类选择器 :checked 可以为选中状态下的 checkbox 设置不同的样式,用以从视觉上区别:

input[type="checkbox"]:checked {
  background-position: -48px 0;
}

此时点击复选框,可以看到复选框样式的变化效果,另外,根据那张设计图片所示还得加上获取焦点,禁用等状态的样式:

input[type="checkbox"]:focus,
input[type="checkbox"]:hover {
  background-position: -24px 0;
  outline: none;
}

input[type="checkbox"]:checked {
  background-position: -48px 0;
}

input[type="checkbox"][disabled] {
  background-position: -72px 0;
}

input[type="checkbox"][disabled]:checked {
  background-position: -96px 0;
}

因为图片已经事先合并成一张了,简单修改一下 background-position 就可以了,同时前面几个选择器的优先级(权重)一样,所以书写顺序很重要。

兼容性

目前只兼容 Webkit 系列浏览器;虽然 Firefox 也实现了替代的 -moz-appearance 属性,不过控件原有的背景颜色、边框样式无法修改,暂时也不大好用;IE 系列暂时不支持该属性,更详细的兼容情况查看 Caniuse/appearance。因此需要为 IE 浏览器清除掉背景图片的影响:


input[type="checkbox"] {
  background: #fff url(i/blue.png);
  background: none\0;
  *background: none;
  ...
}

为了兼容更多的主流浏览器,需要寻求另外的解决方案,在所有主流浏览器里,点击关联某个复选框的 label 时,产生的效果和点击元素自身相同,会切换复选框控件的选中状态。浏览器的这种行为给了我们一个至关重要的挂钩,既然能依靠 label 元素来控制原生复选框的状态,那么就可以不必直接操作实际的复选框元素,而把操作和样式都转移到与之关联的 label 元素上去:

<input id="example" type="checkbox">
<label for="example"></label>

确保 label 元素的 for 属性的值和复选框 input 的 id 值一致,同时将 label 元素放置于 input 之后,这样 CSS 可以通过相邻兄弟选择器(Adjacent sibling selector)定位到这个 label 元素并为之应用样式:


input[type="checkbox"] + label:before {
  background: #fff url(i/blue.png);
  content: " ";
  height: 22px;
  left: 0;
  position: absolute;
  width: 22px;
}

有了样式化的 label 元素来提供交互,原生的 checkbox 控件就显得有点多余了,虽然可以用 display: none 把它隐藏掉,不过隐藏后的表单元素是不能获得焦点的,所以最好的方式还是用 label 元素把它遮住,这样既能支持键盘交互,同时当图片加载失败的时候,又能保证原生的控件可用:


input[type="checkbox"] {
  box-sizing: border-box;
  left: 4px;
  margin: 0;
  padding: 0;
  position: absolute;
  top: 3px;
}

图片要足够大能将原生的 checkbox 控件完全遮挡住,因为这里用到了绝对定位,所以需要增加一个定位参照:


<!-- HTML -->
<div class="checkbox">
  <input id="exampleCheckbox" type="checkbox">
  <label for="exampleCheckbox"></label>
</div>

/* CSS */
.checkbox {
  min-height: 24px;
  padding-left: 24px;
  position: relative;
}

左边预留内边距是为了排版更美观,同时,和之前一样,搭配上其它状态下的样式:


input[type="checkbox"]:focus + label:before,
input[type="checkbox"] + label:hover:before {
  background-position: -24px 0;
}

input[type="checkbox"]:checked + label:before {
  background-position: -48px 0;
}

input[type="checkbox"][disabled] + label:before {
  background-position: -72px 0;
}

input[type="checkbox"][disabled]:checked + label:before {
  background-position: -96px 0;
}

兼容性

只要支持 CSS3 selectors 的浏览器基本上都能兼容,同时具备原生控件的绝大多数交互特性。IE 8 不支持 :checked 伪类选择器,将伪元素:before 修改为双冒号 ::before 可以去掉对 IE 8 的影响:

input[type="checkbox"] + label::before { ... }

关于伪元素生成内容的兼容性见 CSS Generated content for pseudo-elements。诚然,上面的方法假设了支持 :checked 伪类选择器的浏览器同时也支持双冒号伪元素写法,而不支持的浏览器则都不支持,这是一种不太好的方式,这种假设事实上也是不正确的,造成了部分老旧浏览器不可用的问题,如果使用 :not() 选择器则更为合理,使用 :not(:checked) 来为未选中的控件添加样式,:not() 和 :checked 同属一个规范css3-selectors,兼容性应该一致 CSS3 selectors。不过写法有点变化,:checked 和 :not(:checked) 都需要添加上基本的样式:


input[type="checkbox"]:checked + label:before,
input[type="checkbox"]:not(:checked) + label:before {
  background: #fff url(i/blue.png);
  content: " ";
  height: 22px;
  left: 0;
  position: absolute;
  width: 22px;
}

input[type="checkbox"]:not(:checked):focus + label:before,
input[type="checkbox"]:not(:checked) + label:hover:before {
  background-position: -24px 0;
}

input[type="checkbox"]:checked + label:before {
  background-position: -48px 0;
}

input[type="checkbox"][disabled]:not(:checked) + label:before {
  background-position: -72px 0;
}

input[type="checkbox"][disabled]:checked + label:before {
  background-position: -96px 0;
}

查看简单示例,对于那些并不支持 :checked 伪类选择器的浏览器(比如 IE 8),则可以借助 javaScript 来根据控件状态修改真正的 class 属性达到区分不同状态的目的,比如根据是否被选中来添加或删除一个 checked 的 class:


// jQuery
$('input[type="checkbox"]').on('change', function() {
  $(this)[$(this).prop('checked') ? 'addClass' : 'removeClass']('checked');
});

/* CSS */
input[type="checkbox"].checked + label:before { ... }

有了前面的基础,要制作类似于开关按钮的控件也是非常简单的了,还是熟悉的结构:

<div class="checkbox">
  <input id="example" type="checkbox">
  <label for="example">Check</label>
</div>

首先勾勒出开关的形状,一个圆角矩形中间放一个圆形按钮:


input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
  background-color: #e0e0e0;
  border-radius: 24px;
  cursor: pointer;
  display: inline-block;
  height: 24px;
  position: relative;
  text-indent: -9999px;
  width: 48px;
}

input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
  background-color: #fff;
  border-radius: 20px;
  content: " ";
  height: 20px;
  left: 2px;
  position: absolute;
  top: 2px;
  width: 20px;
}

选中的效果只要简单修改下外框的背景色和中间按钮的位置就行:


input[type="checkbox"]:checked + label {
  background-color: #8c8;
}

input[type="checkbox"]:checked + label:after {
  left: 26px;
}

不过这种跳跃式变化实在是太生硬了,添加点过渡效果,看上去更平滑:


input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
  -webkit-transition: background-color 0.3s;
          transition: background-color 0.3s;
}

input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
  -webkit-transition: left 0.3s;
          transition: left 0.3s;
}

点击就能看到效果,对于中间的按钮部分使用 CSS3 Transforms 来替代 left 效果更好,速度更快:


input[type="checkbox"]:checked + label:after,
input[type="checkbox"]:not(:checked) + label:after {
  -webkit-transition: left -webkit-transform 0.3s;
       -o-transition:           -o-transform 0.3s;
          transition: left         transform 0.3s;
}

input[type="checkbox"]:checked + label:after {
  left: 26px;
  -webkit-transform: translateX(24px);
      -ms-transform: translateX(24px);
       -o-transform: translateX(24px);
          transform: translateX(24px);
}

不支持 CSS3 Transforms 的浏览器仍然可以看到背景色的变化,不影响可用性,详见 CSS3 Transforms。关于性能问题,请参考 High Performance Animations。快速点击“控件”会因选中效果造成不能切换状态的情况,所以去掉“控件”可以被选中的能力:

input[type="checkbox"]:checked + label,
input[type="checkbox"]:not(:checked) + label {
  (-prefix-)user-select: none;
}

这里的浏览器厂商前缀根据需要替换成相应的,查看简单示例。当然还需要提供聚焦以及禁用等状态的样式,就不在这里重复了。以上所有技术可同时适用于单选框(radio)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值