本文是继上一篇文章用css3实现惊艳面试官的背景即背景动画(高级附源码)的续篇也是本人最后一篇介绍css3技巧的文章,因为css这块知识难点不是很多,更多的在于去熟悉css3的新特性和基础理论知识。所以写这篇文章的目的一方面是对自己工作中一些css高级技巧的总结,另一方面也是希望能教大家一些实用的技巧和高效开发css的方式,以提高在工作中的效率。
我们将学到
- box-shadow的高级应用
- 制作自适应的椭圆
- 纯css3实现饼图进度动画
- 用border来实现一个对话框样式
- css3 filter的简单应用
- css3伪元素实现自定义复选框
- 在线制作css3动画的利器
正文
1.box-shadow的高级应用
利用css3的新特性可以帮助我们实现各种意想不到的特效,接下来的几个案例我们来使用css3的box-shdow来实现,马上开始吧!
实现水波动画
知识点:box-shadow
想想我们如果不用css3,是怎么实现水波扩散的动画呢?想必一定是写一大堆的js才能实现如下的效果:

css3实现核心代码
1<style> 2.wave { 3 margin-left: auto; 4 margin-right: auto; 5 width: 100px; 6 height: 100px; 7 border-radius: 100px; 8 border: 2px solid #fff; 9 text-align: center; 10 line-height: 100px; 11 color: #fff; 12 background: #06c url(http://p3g4ahmhh.bkt.clouddn.com/me.jpg) no-repeat center center; 13 background-size: 100%; 14 animation: wave 4s linear infinite; 15} 16@keyframes wave { 17 0% { 18 box-shadow: 0 0 0 0 rgba(245, 226, 226, 1), 0 0 0 0 rgba(250, 189, 189, 1); 19 } 20 50% { 21 box-shadow: 0 0 0 20px rgba(245, 226, 226, .5), 0 0 0 0 rgba(250, 189, 189, 1); 22 } 23 100% { 24 box-shadow: 0 0 0 40px rgba(245, 226, 226, 0), 0 0 0 20px rgba(245, 226, 226, 0); 25 } 26} 27</style> 28<div class="wave"></div>
这里我们主要使用了box-shadow的多级阴影来实现的,动画部分我们使用的@keyframes,是不是感觉还行?
实现加载动画
知识点:box-shadow多阴影
加载动画大家想必也不陌生,虽然可以用很多方式实现加载动画,比如用伪元素,用gif,用js,但是更优雅的实现我觉得还是直接上css:

核心代码如下:
1<style> 2.loading { 3 margin-left: auto; 4 margin-right: auto; 5 width: 30px; 6 height: 30px; 7 border-radius: 30px; 8 background-color: transparent; 9 animation: load 3s linear infinite; 10} 11@keyframes load { 12 0% { 13 box-shadow: -40px 0 0 rgba(250, 189, 189, 0), 14 inset 0 0 0 15px rgba(250, 189, 189, 0), 15 40px 0 0 rgba(250, 189, 189, 0); 16 } 17 30% { 18 box-shadow: -40px 0 0 rgba(250, 189, 189, 1), 19 inset 0 0 0 15px rgba(250, 189, 189, 0), 20 40px 0 0 rgba(250, 189, 189, 0); 21 } 22 60% { 23 box-shadow: -40px 0 0 rgba(250, 189, 189, 0), 24 inset 0 0 0 15px rgba(250, 189, 189, 1), 25 40px 0 0 rgba(250, 189, 189, 0); 26 } 27 100% { 28 box-shadow: -40px 0 0 rgba(250, 189, 189, 0), 29 inset 0 0 0 15px rgba(250, 189, 189, 0), 30 40px 0 0 rgba(250, 189, 189, 1); 31 } 32} 33</style> 34<div class="loading"></div>
我们这里也是采用box-shadow多背景来实现,也是我当时思考的一个方向,至于其他的css方案,欢迎大家和我交流。
实现对话框及对话框的不规则投影
知识点: filter和伪元素
这里涉及到css滤镜的知识,不过也很简单,大家在css3官网上看看就理解了,我们直接看效果:

我们会通过filter的drop-shadow来实现不规则图形的阴影,然后利用伪元素和border来实现头部三角形:
1<style> 2.odd-shadow{ 3 margin-left: auto; 4 margin-right: auto; 5 width: 200px; 6 height: 80px; 7 border-radius: 8px; 8 color: #fff; 9 font-size: 24px; 10 text-align: center; 11 line-height: 80px; 12 background: #06c; 13 filter: drop-shadow(2px 2px 2px rgba(0,0,0,.8)) 14} 15.odd-shadow::before{ 16 content: ''; 17 position: absolute; 18 display: block; 19 margin-left: -20px; 20 transform: translateY(20px); 21 width:0; 22 height: 0; 23 border: 10px solid transparent; 24 border-right-color: #06c; 25} 26</style> 27 28<div class="odd-shadow">哎呦,猪先森</div>
模糊效果
知识点: filter
这个比较简单,这里我直接上图和代码:

1filter: blur(20px);
2.制作自适应的椭圆
border-radius的出现让我们实现圆角效果提供了极大的便利,我们还可以通过对Border-radius特性的进一步研究来实现各种图形效果,接下来就让我们看看它的威力吧!
知识点:border-radius: a / b; //a,b分别为圆角的水平、垂直半径,单位若为%,则表示相对于宽度和高度进行解析
核心代码:
1<style> 2.br-1{ 3 width: 200px; 4 height: 100px; 5 border-radius: 50% /10%; 6 background: linear-gradient(45deg,#06f,#f6c,#06c); 7} 8.br-2{ 9 width: 100px; 10 border-radius: 20% 50%; 11} 12.ani{ 13 animation: skew 4s infinite; 14} 15.ani1{ 16 animation: skew1 4s infinite 2s; 17} 18.ani2{ 19 animation: skew2 4s infinite 3s; 20} 21@keyframes skew{ 22 to{ 23 border-radius: 50%; 24 } 25} 26@keyframes skew1{ 27 to{ 28 border-radius: 20px 20px 100%; 29 } 30} 31@keyframes skew2{ 32 to{ 33 transform: rotate(360deg); 34 } 35} 36</style> 37<div class="br-1 black-theme"></div> 38<div class="br-1 black-theme ani"></div> 39<div class="br-1 black-theme ani1"></div> 40<div class="br-1 br-2 black-theme ani2"></div>
这里我们主要使用了背景渐变来实现华而不实的背景,用border-radius实现各种规格的椭圆图案。
3.纯css3实现饼图进度动画
知识点:border-radius: a b c d / e f g h; animation多动画属性;
效果如下:

核心代码:
1<style> 2.br-31{ 3 width: 100px; 4 height: 100px; 5 border-radius: 50%; 6 background: linear-gradient(to right,#f6c 50%,#333 0); 7} 8.br-31::before{ 9 content: ''; 10 display: block; 11 margin-left: 50%; 12 height: 100%; 13 border-radius: 0 100% 100% 0 / 50%; 14 background-color: #f6c; 15 transform-origin: left; 16 animation: skin 4s linear infinite, 17 bg 8s step-end infinite; 18} 19@keyframes skin{ 20 to{ 21 transform: rotate(.5turn); 22 } 23} 24@keyframes bg{ 25 50%{ 26 background: #333; 27 } 28} 29.br-32::before{ 30 animation-play-state: paused; 31 animation-delay: inherit; 32} 33</style> 34<div class="br-31 black-theme"></div> 35<div class="br-31 br-32 black-theme" style="animation-delay:-1s"></div>
这块的实现我们主要用了渐变背景,也是实现扇形进度的关键,包括代码中的如何遮挡半圆,如何对半圆做动画,如何改变旋转原点的位置等,这些虽然技巧性很强,但是我们稍微画一画,也可以实现的。
4.css3伪元素实现自定义复选框
我们都知道原生的复选框控件样式极难自定义,这对于工程师实现设计稿的难度加大了一大截。css3的出现,增加了:checked选择器,因此我们可以利用:checked和label来实现各式各样的表单选择控件,接下来让我们来看看如何实现吧!
我们来看看如何实现上述自定义的复选框:
1<style> 2.check-wrap{ 3 text-align: center; 4} 5.checkbox{ 6 position: absolute; 7 clip: rect(0,0,0,0); 8} 9.checkbox[type="checkbox"]:focus + label::before{ 10 box-shadow: 0 0 .6em #06c; 11} 12.checkbox[type="checkbox"] + label::before{ 13 content: '\a0'; /* 不换行空格 */ 14 display: inline-block; 15 margin-right: .3em; 16 width: 2em; 17 height: 2em; 18 border-radius: .3em; 19 vertical-align: middle; 20 line-height: 2em; /* 关键 */ 21 font-size: 20px; 22 text-align: center; 23 color: #fff; 24 background: gray; 25} 26.checkbox[type="checkbox"]:checked + label::before{ 27 content: '\2713'; /* 对勾 */ 28 background: black; 29} 30 31label{ 32 margin-right: 40px; 33 font-size: 20px; 34} 35</style> 36<div class="check-wrap"> 37 <input type="checkbox" class="checkbox" id="check-1" /> 38 <label for="check-1">生男孩</label> 39 <input type="checkbox" class="checkbox" id="check-2" /> 40 <label for="check-2">生女孩</label> 41</div>
这里为了隐藏原生的checkbox控间,我们用了clip: rect(0,0,0,0)进行截取,然后使用checkbox的伪类:checked来实现交互。
接下来扩展一下,我们来实现自定义开关:

这里原理是一样的,只不过样式做了改动,直接上代码:
1<style> 2.check-wrap{ 3 margin-bottom: 20px; 4 text-align: center; 5} 6.switch{ 7 position: absolute; 8 clip: rect(0,0,0,0); 9} 10 11.switch[type="checkbox"] + label{ 12 width: 6em; 13 height: 3em; 14 padding: .3em; 15 border-radius: .3em; 16 border: 1px solid rgba(0,0,0,.2); 17 vertical-align: middle; 18 line-height: 2em; /* 关键 */ 19 font-size: 20px; 20 text-align: center; 21 color: #fff; 22 box-shadow: 0 1px white inset; 23 background-color: #ccc; 24 background-image: linear-gradient(#ddd,#bbb); 25} 26.switch[type="checkbox"]:checked + label{ 27 box-shadow: 0.05em .1em .2em rgba(0,0,0,.6) inset; 28 border-color: rgba(0,0,0,.3); 29 background: #bbb; 30} 31 32label{ 33 margin-right: 40px; 34 font-size: 14px; 35} 36 37.switch-an{ 38 position: absolute; 39 clip: rect(0,0,0,0); 40} 41 42.switch-an[type="checkbox"] + label{ 43 position: relative; 44 display: inline-block; 45 width: 5em; 46 height: 2em; 47 border-radius: 1em; 48 color: #fff; 49 background: #06c; 50 text-align: left; 51} 52 53.switch-an[type="checkbox"] + label::before{ 54 content: ''; 55 width:2em; 56 height: 2em; 57 position: absolute; 58 left: 0; 59 border-radius: 100%; 60 vertical-align: middle; 61 background-color: #fff; 62 transition: left .3s; 63} 64.switch-an[type="checkbox"] + label::after{ 65 content: 'OFF'; 66 margin-left: 2.6em; 67} 68.switch-an[type="checkbox"]:checked + label::before{ 69 transition: left .3s; 70 left: 3em; 71} 72.switch-an[type="checkbox"]:checked + label::after{ 73 content: 'NO'; 74 margin-left: .6em; 75} 76 77</style> 78<div class="check-wrap"> 79 <input type="checkbox" class="switch" id="switch-1" /> 80 <label for="switch-1">生男孩</label> 81 <input type="checkbox" class="switch" id="switch-2" /> 82 <label for="switch-2">生女孩</label> 83</div> 84 85<div class="check-wrap"> 86 <input type="checkbox" class="switch-an" id="switch-an-1" /> 87 <label for="switch-an-1"></label> 88</div>
是不是感觉css3提供了更强大的动画和自定义功能呢?其实我们可以实现更酷炫更实用的效果,等待你去尝试。
5.在线制作css3动画的利器
最后推荐一个在线制作各种贝塞尔曲线的工具,也是本人在做动画时经常使用的: cubic-bezier。
最后
笔者2天后将推出开源的CMS系统,技术架构:
- 后台Node+Koa+redis+JsonSchema
- 管理后台界面 vue-cli3 + vue + ts + vuex + antd-vue + axios
- 客户端前台 react + antd + react-hooks + axios
后面将推出该系统的设计思想,架构和实现过程,欢迎在公众号《趣谈前端》里查看更详细的介绍。
欢迎大家相互学习交流,一起探索前端的边界。
更多推荐
- 用css3实现惊艳面试官的背景即背景动画(高级附源码)
- 记一次老项目中的跨页面通信问题和前端实现文件下载功能
- 如何优雅的使用javascript递归画一棵结构树
- 5分钟教你用nodeJS手写一个mock数据服务器
- 笛卡尔乘积的javascript版实现和应用
- JavaScript 中的二叉树以及二叉搜索树的实现及应用
- 用 JavaScript 和 C3 实现一个转盘小游戏
- 教你用200行代码写一个爱豆拼拼乐H5小游戏(附源码)
- 基于react/vue生态的前端集成解决方案探索与总结
- 9012教你如何使用gulp4开发项目脚手架
- 如何用不到200行代码写一款属于自己的js类库)
- 让你瞬间提高工作效率的常用js函数汇总(持续更新)
- 一张图教你快速玩转vue-cli3
- 3分钟教你用原生js实现具有进度监听的文件上传预览组件
- 3分钟教你用原生js实现具有进度监听的文件上传预览组件
- 使用Angular8和百度地图api开发《旅游清单》
- js基本搜索算法实现与170万条数据下的性能测试
- 《前端算法系列》如何让前端代码速度提高60倍
- 《前端算法系列》数组去重
- vue高级进阶系列——用typescript玩转vue和vuex
- 前端三年,谈谈最值得读的5本书籍
