果图:
移入:
在之前的一篇文章《默认的input标签太丑?教你如何使用CSS写出带动画的样式》中有讲过如何实现一个漂亮的input标签样式。
既然已经折腾了,今天这篇文章我们就继续折腾,看看如何通过CSS3实现一个带动画的checkbox样式?
CSS3
感兴趣的可以自行去github上看源文件,地址如下:
https://github.com/zhouxiongking/article-pages/blob/master/articles/beautifulCheckbox/beautifulCheckbox.html
checkbox作为多选框,在form表单中的使用频率是非常高的,例如问卷调查的多选题,个人兴趣爱好的选择时都会用到多选框。
而浏览器默认的checkbox样式都很丑,看起来就是一个小小的方框,虽然Bootstrap已经有好看的checkbox样式,但是它不支持动画,在交互上仍然存在缺陷,所以我们就试图自己写出一个带动画的checkbox效果。
首先我们来看看checkbox的动态效果图(中间的黑色圆圈实际为录制GIF图时产生的,请忽略)
checkbox效果图
接下来我们具体分析下页面的元素组成,主要包括以下部分。
默认的checkbox标签,将其display设置为none,隐藏起来。
label标签,表现为实际显示的动态选中和未选中效果。
我们需要达到的效果是,在未选中checkbox时,默认为灰色的边框,白色的背景;在选中checkbox时,改变背景色,出现白色的打勾动画效果。
在这部分,我们来具体分析下页面的代码组成。
HTML
首先是页面的HTML部分代码,如上一部分所示,页面其实只有两个元素组成,这两个元素放在一个容器下,故HTML代码如下所示。
HTML部分代码
CSS
我们来看看CSS部分的代码,因为CSS部分代码比较多,我们一一来分析。
首先是外层container的样式,同时对页面的html和body也做一些简单处理,代码如下所示。
container样式
其次是checkbox标签的样式,因为原生的checkbox除了在传输数据时有用处,在页面呈现上并没有作用,故设置display:none;将其隐藏。
checkbox样式
然后是最重要的label部分,在未点击之前label呈现一个正方形,有基本的样式。
基本样式
在点击方形框后,出现的对勾是通过伪元素::before和::after实现的,对勾实际为两个矩形拼接而成,左侧的矩形用::after元素表示,右侧的矩形用::before元素表示。两者的通用样式通过如下代码实现。
伪元素实现
然后是针对::before和::after所特有的样式,因为两者的位置和旋转的角度不同,代码也会出现差异。
特有样式
然后是定义的打钩的动画效果,这里使用keyframes关键字定义,考虑到浏览器兼容性,定义的动画具有不同的前缀。定义的动画也包括两部分,首先是右侧的矩形动画效果。
右侧矩形动画效果
然后是左侧矩形的动画效果,同样考虑到浏览器的兼容性,样式代码如下。
左侧矩形动画
至此,所有代码都已讲解完毕,如果运行之后就会看到文章开始的动画效果。
本篇文章主要讲的是如何使用自定义的动画完成checkbox效果,感兴趣的可以直接去github上看源文件代码,后续会写一个跟开关switch有关的文章,敬请期待~
adio、checkbox和switch应该是一个比较常用的html标签,尤其是在中后台ERP系统里面更为常见。不过浏览器自带的样式不怎么好看,而且不同浏览器效果也不一样。出于美化和统一视觉效果的需求,自定义样式就被提出来了。
纯css实现的主要手段是利用label标签的模拟功能。label的for属性可以关联一个具体的input元素,即使这个input本身不可被用户可见,有个与它对应的label后,用户可以直接通过和label标签交互来替代原生的input——而这给我们的样式模拟留下了空间。简而言之就是:
隐藏原生input,样式定义的过程留给label (那为什么不直接改变checkbox的样式?因为checkbox作为浏览器默认组件,样式更改上并没有label那么方便,很多属性对checkbox都是不起作用的,比如background,而label在样式上基本和div一样'任人宰割')
而在选择事件上,由于css的“相邻选择符(E+F)”的存在,让我们可以直接利用html的默认checkbox,免去了js模拟选择的麻烦。
DEMO的部分CSS3属性只写了webkit前缀,所以建议用webkit内核的浏览器查看本页案例,当然只要你给样式补上对应的浏览器前缀,就可以实现更多样式匹配
HTML代码:
<!-- input的id必须有,这个是label进行元素匹配所必需的 --> <!-- 可以看到每个input的id和label的“for”属性对应同一字符串 --> <input type="checkbox" id="checkbox01" /> <label for="checkbox01"></label> <input type="checkbox" id="checkbox02" /> <label for="checkbox02"></label> <input type="checkbox" id="checkbox03" /> <label for="checkbox03"></label> <input type="checkbox" id="checkbox04" /> <label for="checkbox04"></label>
HTML构建完成,接下来是对应的css:
/* 隐藏所有checkbox */ input[type='checkbox'] { display: none; } /* 对label进行模拟.背景图片随便拼凑的,不要吐槽品味*/ /* transition效果是做个背景切换效果,这里单纯演示而已,实际上这个过渡不加更自然*/ label { display: inline-block; width: 60px; height: 60px; position: relative; background: url(//www.chitanda.me/images/blank.png); background-position: 0 0px; -webkit-transition: background 0.5s linear; } /* 利用相邻选择符和checkbox`:checked`的状态伪类来模拟默认选中效果(就是点击后那个勾号的效果) */ /*如果这段代码注释,点击后将没有任何反馈给用户*/ /*因为label本身是没有点击后被选中的状态的,checkbox被隐藏后,这个状态只能手动模拟*/ input[type='checkbox']:checked+label { background-position: 0 -60px; }
上面代码看起来好像也可以了。不过仔细想想,貌似缺了点什么:选项对应的提示文字
对css不了解的新人可能这时候第一反应就是在label后面用p标签或者span标签来添加文字。不过这种方式都不怎么优雅。个人建议用css的::before和::after伪元素(::before和:before是一个东西。不过为了把“伪元素”和“伪类”区分出来,W3C建议的写法是伪元素用::而伪类用:)
/* 伪元素的生效很简单,定义`content`就好,其余的属性和普通div一样 */ label::after { content: attr(data-name); /*利用attr可以减少css代码量,data-name写在html部分的label属性里*/ display: inline-block; position: relative; width: 120px; height: 60px; left: 100%; vertical-align: middle; margin: 10px; }
当然既然可以用::after模拟label的文字,那也就可以用::before模拟label的checkbox样式,这里就不做解析了。
这里提一下伪类和伪元素的区分:
1)伪类:存在的意义是为了通过选择器找到那些不存在于DOM树中的信息以及不能被常规CSS选择器获取到的信息。 伪类由一个冒号:开头,冒号后面是伪类的名称和包含在圆括号中的可选参数。
常用的伪类:
:active 向被激活的元素添加样式。 :focus 向拥有键盘输入焦点的元素添加样式。 :hover 当鼠标悬浮在元素上方时,向元素添加样式。 :link 向未被访问的链接添加样式。 :visited 向已被访问的链接添加样式。 :first-child 向元素的第一个子元素添加样式。 :checked 向选中的控件元素添加样式
2)伪元素:伪元素在DOM树中创建了一些抽象元素,这些抽象元素是不存在于文档语言里的(可以理解为html源码);
注意: css3为了区分伪类和伪元素,规定伪类前面有一个冒号,伪元素前面有两个冒号
常用伪元素:
关于伪元素的讲解,可以参考CSS伪类与伪元素总是傻傻分不清,这份总结够面试用了
::before 为作用元素的第一个子节点插入dom中 ::after 为作用元素的最后一个子节点插入dom中
自定义radio
html代码:
<input type="radio" id="radio"> <label for="radio"></label>
css代码:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; border-radius: 50%; position: relative; } label::after { -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; width: 16px; height: 16px; border-radius: 50%; top: 50%; left: 50%; margin-top:-8px; margin-left:-8px; z-index: 1; content: ''; border:1px solid #333; } input:checked+label::after{ background:red; }
实现效果:
点击前和点击后:
自定义checkbox
漂亮的checkbox长这样的,看着就很可爱
我们可以不追求那么完美的样式,可以实现下面简单好看的样式就可以
html代码:
<input type="checkbox" id="checkbox"> <label for="checkbox"></label>
css代码:
input{ display:none; } label { display: inline-block; width: 30px; height: 30px; border: 1px solid #333; position: relative; } label::after { -webkit-transition: opacity .5s ease; -moz-transition: opacity .5s ease; -o-transition: opacity .5s ease; -ms-transition: opacity .5s ease; transition: opacity .5s ease; cursor: pointer; position: absolute; content: ''; opacity: 0; } input:checked+label::after{ border: 2px solid #d73d32; border-top: none; border-right: none; -webkit-transform: rotate(-45deg); -ms-transform: rotate(-45deg); transform: rotate(-45deg); width:20px; height:10px; top:50%; margin-top:-8px; left:50%; margin-left:-10px; opacity: 1.0; }
实现效果:
点击前和点击后:
自定义switch
继续分享一个iOS风格的switch开关按钮,样子也非常常见,如图:
主要是使用了<input ?type="checkbox">来模拟实现,具体的HTML:
html 代码:
<label><input class="mui-switch" type="checkbox"> 默认未选中</label> <label><input class="mui-switch" type="checkbox" checked> 默认选中</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label> <label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label> <label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim 类即可</label> <label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>
在实际的使用中后来又增加了两个过渡效果,分别加?mui-switch-animbg和mui-switch-anim?类即可,具体效果查看下面的demo页面。
CSS代码(SCSS导出的,排版有些奇怪):
css 代码:
剩下部分
这里给出具体的css,方便大家复制本地实现
<style> .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; } .mui-switch:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; border-radius: 20px; border-top-left-radius: 20px; border-top-right-radius: 20px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } .mui-switch:checked { border-color: #64bd63; box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; } .mui-switch:checked:before { left: 21px; } .mui-switch.mui-switch-animbg { transition: background-color ease 0.4s; } .mui-switch.mui-switch-animbg:before { transition: left 0.3s; } .mui-switch.mui-switch-animbg:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: #64bd63; transition: border-color 0.4s, background-color ease 0.4s; } .mui-switch.mui-switch-animbg:checked:before { transition: left 0.3s; } .mui-switch.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; } .mui-switch.mui-switch-anim:before { transition: left 0.3s; } .mui-switch.mui-switch-anim:checked { box-shadow: #64bd63 0 0 0 16px inset; background-color: #64bd63; transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; } .mui-switch.mui-switch-anim:checked:before { transition: left 0.3s; } /*# sourceMappingURL=mui-switch.css.map */ </style>
如果你喜欢scss,那么代码更加简洁
@mixin borderRadius($radius:20px) { border-radius: $radius; border-top-left-radius: $radius; border-top-right-radius: $radius; border-bottom-left-radius: $radius; border-bottom-right-radius: $radius; } $duration: .4s; $checkedColor: #64bd63; .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-color: #fdfdfd; box-shadow: #dfdfdf 0 0 0 0 inset; @include borderRadius(); background-clip: content-box; display: inline-block; -webkit-appearance: none; user-select: none; outline: none; &:before { content: ''; width: 29px; height: 29px; position: absolute; top: 0px; left: 0; @include borderRadius(); background-color: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } &:checked { border-color: $checkedColor; box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; &:before { left: 21px; } } &.mui-switch-animbg { transition: background-color ease $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: #dfdfdf 0 0 0 0 inset; background-color: $checkedColor; transition: border-color $duration, background-color ease $duration; &:before { transition: left 0.3s; } } } &.mui-switch-anim { transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration; &:before { transition: left 0.3s; } &:checked { box-shadow: $checkedColor 0 0 0 16px inset; background-color: $checkedColor; transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3; &:before { transition: left 0.3s; } } } }
链接文章
https://www.html.cn/archives/9274
https://segmentfault.com/a/1190000003711140
*请认真填写需求信息,我们会在24小时内与您取得联系。