原文连接感謝 comehope 大佬的 [前端每日实战]css
工做三个月以为糟糕跑路(顺带劝一下像我同样的新人, 不要急于入职, 必定要挑一挑)回家从新补充了一下基础知识及 node, 身为一个前端结果发现 CSS 已经手生了, 为了明年可以找到工做, 因此又开始练习了...html
github.io 浏览
https://github.com/shanyuhai1...前端
html
结构咱们须要五颗爱心及底部的 footernode
<figure class="hearts"> <section class="heart"></section> <section class="heart"></section> <section class="heart"></section> <section class="heart"></section> <section class="heart"></section> </figure> <footer>pink hearts</footer>
样式初始化及居中git
body { margin: 0; height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; background-color: #f3f3f3; overflow: hidden; } .hearts { width: 100vw; height: 20vw; border: 1px solid; /* the snippet will be deleted */ box-sizing: border-box; padding: 0 5vw; display: flex; align-items: center; justify-content: space-between; } .heart { width: 15vw; height: 15vw; border: 1px solid; /* the snippet will be deleted */ } footer { margin-top: 10vh; text-transform: uppercase; letter-spacing: 2px; font-family: "verdana"; font-size: 22px; color: #F48FB1; }
接着在第一个 heart 中添加一个粉红的正方形github
添加 DOM 结构segmentfault
<section class="heart"> <div class="plane"> <div class="half-heart"></div> </div> </section>
基准面(plane) 定位并完成基础样式flex
.heart { position: relative; display: flex; align-items: center; justify-content: center; } .plane { position: absolute; opacity:0.8; } .half-heart { width: 7.5vw; height: 7.5vw; background-color: pink; transform: rotate(-45deg); }
一颗爱心由两个基准面组成动画
<section class="heart"> <div class="plane plane-left"> <div class="half-heart"></div> </div> <div class="plane plane-right"> <div class="half-heart"></div> </div> </section>
.heart { transform-style: preserve-3d; } .plane-right { transform: rotateY(90deg); /* 由于此处为 90 度垂直, 因此并不可见 */ }
接着添加旋转动画(这样咱们就能够看到两个基准面了)spa
.heart { animation: rotate 5s ease-in-out infinite; } .heart:nth-of-type(1) { animation-delay:-5s; } /* keyframes */ @keyframes rotate { 0% { transform: rotateY(0deg) rotateZ(25deg) translateY(7.5vw); } 50% { transform: rotateY(270deg) rotateZ(25deg) translateY(-7.5vw); } 100% { transform: rotateY(360deg) rotateZ(25deg) translateY(7.5vw); } }
生成两个圆形放置在正方的上方便可(伪元素可解决)
.half-heart:before, .half-heart:after { content: ""; width: 7.5vw; height: 7.5vw; background-color: pink; border-radius: 100%; position: absolute; } .half-heart:before { top: -3.25vw; left: 0; } .half-heart:after { top: 0; left: 3.25vw; }
好了, 这样一个旋转上升的爱心就完成了
修改延迟时间(DOM 结构省略)
.heart:nth-of-type(1) { animation-delay:-5s; } .heart:nth-of-type(2) { animation-delay:-4s; } .heart:nth-of-type(3) { animation-delay:-3s; } .heart:nth-of-type(4) { animation-delay:-2s; } .heart:nth-of-type(5) { animation-delay:-1s; }
最后记得把以前确认位置及大小的 border 边框删除便可