按下右侧的“点击预览”按钮在当前页面预览,点击连接全屏预览。css
https://codepen.io/zhang-ou/pen/ELWMLrhtml
此视频是能够交互的,你能够随时暂停视频,编辑视频中的代码。git
请用 chrome, safari, edge 打开观看。github
https://scrimba.com/c/cqrNEsmchrome
请从 github 下载。dom
https://github.com/comehope/front-end-daily-challenges/tree/master/009-aimed-button-effectsflex
定义 dom,容器中包含 5 个 span,第 1 个是按钮文字,另 4 个用来修饰:动画
<div class="box"> <span>BUTTON</span> <span class="top"></span> <span class="bottom"></span> <span class="left"></span> <span class="right"></span> </div>
居中显示:spa
html, body { height: 100%; display: flex; align-items: center; justify-content: center; background-color: black; color: silver; }
设置文字样式:code
.box { width: 9em; height: 3em; font-size: 30px; text-align: center; line-height: 3em; letter-spacing: 0.2em; font-family: sans-serif; }
画出瞄准镜中间的圆圈:
.box { position: relative; } .box::after { content: ''; position: absolute; width: 3em; height: 3em; border: 1px solid red; border-radius: 50%; left: 3em; }
画出瞄准镜的十字坐标线:
.box span:not(:first-child) { position: absolute; background-color: red; } .box span.top, .box span.bottom { width: 1px; height: 3em; left: 50%; } .box span.top { top: -3em; } .box span.bottom { bottom: -3em; } .box span.left, .box span.right { width: 3em; height: 1px; top: 50%; } .box span.left { left: 0; } .box span.right { right: 0; }
定义瞄准动画:
@keyframes aim { from { filter: opacity(0.2); } to { filter: opacity(0.8); } }
应用瞄准动画到瞄准镜上:
.box::after { filter: opacity(0); } .box span:not(:first-child) { filter: opacity(0); } .box:hover::after, .box:hover span:not(:first-child) { animation: aim 1s linear infinite alternate; }
最后,为容器设置从模糊到清晰的缓动效果:
.box { filter: blur(2px); transition: 0.5s; } .box:hover { filter: blur(0.2px); }
大功告成!