CSS动效集锦,视觉魔法的碰撞与融合(三)
目录
正文
- 扇形 DIV 的使用——实现雷达扫描图
- DIV 环形布局—实现 loading 圈
- 动画的向量合成—实现抛物线动画
- 无限滚动动画—实现跑马灯效果
- perspective 和 transform 的运用——实现卡片翻转
扇形 DIV 的使用——实现雷达扫描图
- 锐角扇形:deg<0,向右边倾斜,即可得到锐角扇形
- 钝角扇形:deg>0, 向左边倾斜,即可得到钝角扇形
// CSS 代码 @keyframes rotateAnimate { from { transform: rotate(0deg) skew(-30deg) }to {
transform: rotate(360deg) skew(-30deg)
}
}.fan-wrapper {
overflow: hidden;
position: relative;
margin: 100px;
width: 200px;
height: 200px;
border-radius: 50%;
background: red;
}.fan {
position: absolute;
right: 0;
animation: rotateAnimate 2s linear infinite;
/ 这一行很重要,设置左下角为旋转点 /
transform-origin: 0% 100%;
width: 100px;
height: 100px;
background: blue;
}
// HTML 代码
<div class="fan-wrapper">
<div class="fan"></div>
</div>
DIV 环形布局—实现 loading 圈
// CSS 代码 .circles { position: relative; margin: 50px; width: 200px; height: 200px; }.circle {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background: black;
}
// HTML
<div class="circles">
<div class="circle circle1"></div>
<div class="circle circle2"></div>
<div class="circle circle3"></div>
<div class="circle circle4"></div>
<div class="circle circle5"></div>
<div class="circle circle6"></div>
<div class="circle circle7"></div>
<div class="circle circle8"></div>
</div>
/** * R: 大圆半径,2*R = 外部正方形的边长 * r: 在大圆边上等距排列的小圆的半径 * counts: 圆的数量 * 返回值: * [* [x1,y1], * [x2,y2], * ... * ] */ function calcXYs(R, r, counts) { // 当前度数 let deg = 0; // 单位度数,两小圆和圆心的夹角 const pDeg = 360 / counts; // 存放返回结果 const arr = []; for (let i = 0; i < counts; i++) { // 度数以单位度数递增 deg = pDeg * i; // Math.sin 接收的参数以 π 为单位,需要根据 360 度 = 2π进行转化 const proportion = Math.PI / 180; // 以外部 DIV 左下角为原点,计算小圆圆心的横纵坐标 let Y = R + R * Math.sin(proportion * deg); let X = R + R * Math.cos(proportion * deg); // 存放结果 arr.push([X, Y, deg]); } return arr; }
/** * R,r,counts:含义同上 * selector: 获取所有小圆的标志符 * 作用:根据上一步的坐标计算结果,调整绝对定位的小圆的位置 */ function resizeCircles(selector, R, r, counts) { // 获取所有小圆 NodeList 的选择器 let list = document.querySelectorAll(selector); //调用 calcXYs 方法 const XYs = calcXYs(R, r, counts); // 遍历每个小圆的 XY 坐标 for (let i = 0; i < list.length; i++) {const [X, Y] = XYs[i]; const e = list[i]; // 修改小圆距离外部 DIV 底部和左边的距离 e.style.left = X + "px"; e.style.bottom = Y + "px"; } }
resizeCircles(".circle", 60, 20, 8);
- 给每个圆设置 animation 实现明暗变化,例如可以设置黑色的背景色然后动态变化 opacity
- animation 属性可以设置 delay 实现动画延迟播放,我们依次给圆设置等距的 delay,例如 1s,2s,3s...
- 给 animation 属性设置 alternate,表示往复播放,设置 infinite,表示无限循环播放
@keyframes k { from { opacity: 1; }to {
opacity: 0;
}
}
.circle1 {
animation: k 1s ease 0s alternate infinite;
}.circle2 {
animation: k 1s ease 0.2s alternate infinite;
}.circle3 {
animation: k 1s ease 0.4s alternate infinite;
}
// circle4 ~ circle8 同理,delay 以 0.2s 递增
动画的向量合成—实现抛物线动画
- 水平方向: 匀速直线运动
- 垂直方向:初速度为 0 的匀加速直线运动
- 设置两个 div:外层 div 和内层 div
- 外层 div 设置横向匀速运动的动画
- 内层 div 设置纵向的匀加速直线运动的动画,加速过程可以用 cubic-bezier 设置
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1);
// HTML <div id="outer"> <div id="inner"></div> </div> <button id='btn'> 抛物线效果 </button>
// CSS #outer { transition: all 1.5s linear; }#inner {
width: 30px;
height: 30px;
border-radius: 50%;
background: red;
transition: all 1.5s cubic-bezier(.54, .11, .95, .68);
}.outer-active {
transform: translateX(300px);
}.inner-active {
transform: translateY(300px) scale(0.3);
}
JS
document.getElementById("btn").onclick = function() { document.getElementById("outer").classList.add("outer-active"); document.getElementById("inner").classList.add("inner-active");};
无限滚动动画—实现跑马灯效果
// HTML <div class="marquee"> <p>ABCDEFGHIJKLMN</p> </div> // CSS @keyframes marquee { from { transform: translateX(-200px) }to {
transform: translateX(200px)
}
}.marquee {
overflow: hidden;
margin: 100px;
width: 200px;
}.marquee p {
animation: marquee 3s linear infinite;
}
perspective 和 transform 的运用——实现卡片翻转
- transform: rotateY(x deg) 翻转卡片
- backface-visibility:hidden 翻转后隐藏背面,重要!必须要加
- perspective:增加透视和立体效果
// HTML
<div id="img-wrapper">
<img src='./timg.jpg' id='img1' class="img disable-img1" />
<img src='./timg2.jpg' id='img2' class="img" />
</div>
// CSS
#img-wrapper {
perspective: 1200px;
position: relative;
height: 479px;
}
#img1,
#img2 {
position: absolute;
transition: all 1s linear;
backface-visibility: hidden;
}
#img1 {
transform: rotateY(-180deg);
}
#img-wrapper:hover #img1 {
transform: rotateY(-360deg);
}
#img-wrapper:hover #img2 {
transform: rotateY(-180deg);
}