首页 技术 正文
技术 2022年11月6日
0 收藏 499 点赞 335 浏览 4268 个字

<!–
.spinner {
margin: 100px;
width: 50px;
height: 60px;
text-align: center;
font-size: 10px;
}

.spinner >–> div {
background-color: #67CF22;
height: 100%;
width: 6px;
display: inline-block;

-webkit-animation: stretchdelay 1.2s infinite ease-in-out;
animation: stretchdelay 1.2s infinite ease-in-out;
}

.spinner .rect2 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}

.spinner .rect3 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}

.spinner .rect4 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}

.spinner .rect5 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}

@-webkit-keyframes stretchdelay {
0%, 40%, 100% { -webkit-transform: scaleY(0.4) }
20% { -webkit-transform: scaleY(1.0) }
}

@keyframes stretchdelay {
0%, 40%, 100% {
transform: scaleY(0.4);
-webkit-transform: scaleY(0.4);
} 20% {
transform: scaleY(1.0);
-webkit-transform: scaleY(1.0);
}
}
–>

  在写h5页面中,我们可能经常遇到要写很多css3动画,有没有同学遇到多个物件同样的动画效果,循环保存步调一致呢,我就经常碰到,之前一直不知道其中的原理,只是简单的迁移改改,可是遇到稍微复杂多一点的就hold不住了,只能硬着头皮去分析其中的原理。那么接下来,让我们先看看案例,然后了解之中的原理

css3动画循环案例

案例一:loading动画效果

写多个物件css3循环动画案例原理

html代码:

<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>

css样式:

<style>
.spinner {
margin: 100px;
width: 50px;
height: 60px;
text-align: center;
font-size: 10px;
}.spinner > div {
background-color: #67CF22;
height: %;
width: 6px;
display: inline-block; -webkit-animation: stretchdelay .2s infinite ease-in-out;
animation: stretchdelay .2s infinite ease-in-out;
}.spinner .rect2 {
-webkit-animation-delay: -.1s;
animation-delay: -.1s;
}.spinner .rect3 {
-webkit-animation-delay: -.0s;
animation-delay: -.0s;
}.spinner .rect4 {
-webkit-animation-delay: -.9s;
animation-delay: -.9s;
}.spinner .rect5 {
-webkit-animation-delay: -.8s;
animation-delay: -.8s;
}@-webkit-keyframes stretchdelay {
%, %, % { -webkit-transform: scaleY(0.4) }
% { -webkit-transform: scaleY(1.0) }
}@keyframes stretchdelay {
%, %, % {
transform: scaleY(0.4);
-webkit-transform: scaleY(0.4);
} % {
transform: scaleY(1.0);
-webkit-transform: scaleY(1.0);
}
}
</style>

案例二:圆形放大或缩小的loading效果

写多个物件css3循环动画案例原理

html代码:

<div class="spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>

css样式:

.spinner {
width: 60px;
height: 60px; position: relative;
margin: 100px;
}.double-bounce1, .double-bounce2 {
width: %;
height: %;
border-radius: %;
background-color: #67CF22;
opacity: 0.6;
position: absolute;
top: ;
left: ; -webkit-animation: bounce .0s infinite ease-in-out;
animation: bounce .0s infinite ease-in-out;
}.double-bounce2 {
-webkit-animation-delay: -.0s;
animation-delay: -.0s;
}@-webkit-keyframes bounce {
%, % { -webkit-transform: scale(0.0) }
% { -webkit-transform: scale(1.0) }
}@keyframes bounce {
%, % {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} % {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}

  

  看了上面的两个案例,我们知道了什么是多个物件循环动画,也可以知道,通过animation-delay延迟,我们可以让多个物体保持步调一致的运动效果,但是看了动画和延迟却还是不知道怎么设置,可能你会想象物体运行的帧,确实我分析的时候也是这么想的。

css3动画循环原理

  要讲清楚这个,我们得先知道几个概率(参数),animation动画持续时间t1,每个物体延迟时间差t2,物体个数n

  t2 = t1/n

  平均关键帧的百分比为 100%/n

  把以上的这几个点,理解清楚了之后,其实我们应该就清楚了怎么去写多个物体循环了,举个例子:

html代码:

      <ul>
<li class="on1"></li>
<li class="on2"></li>
<li class="on3"></li>
<li class="on4"></li>
<li class="on5"></li>
<li class="on6"></li>
</ul>

css代码:

        ul{list-style:none;padding:;margin:;}
ul>li{
width:50px;
height:50px;
margin-bottom:20px;
background:orange;
-webkit-animation:scale 3s linear infinite;
}
.on2{
-webkit-animation-delay:.5s;
}
.on3{
-webkit-animation-delay:1s;
}
.on4{
-webkit-animation-delay:.5s;
}
.on5{
-webkit-animation-delay:2s;
}
.on6{
-webkit-animation-delay:.5s;
}
@-webkit-keyframes scale{
%,33.4%{width:50px;height:50px;}
16.7%{width:80px;height:80px;}
}

  这个例子其实是6个物体循环的动画,我们控制在每次100%/6的点做不同物体变化效果就行了,即16.7%、33.4%、50.1%、66.8%、83.5、100.2%(这里由于不整除,多出了一部分,就当近似于)

  而且,我们想一开始就看到元素开始动,那么我们就要将这16.7%前置,则得出了

      @-webkit-keyframes scale{
%,33.4%{width:50px;height:50px;}
16.7%{width:80px;height:80px;}
}

  最后可能也不是讲的很清楚,还请不理解的多做几个例子,多想想就通了,共勉。

  课外知识:

  CSS3 animation-direction 属性

定义和用法

animation-direction 属性定义是否应该轮流反向播放动画。

如果 animation-direction 值是 “alternate”,则动画会在奇数次数(1、3、5 等等)正常播放,而在偶数次数(2、4、6 等等)向后播放。

注释:如果把动画设置为只播放一次,则该属性没有效果。

默认值: normal
继承性: no
版本: CSS3
JavaScript 语法: object.style.animationDirection=”alternate”

语法

animation-direction: normal|alternate;
描述 测试
normal 默认值。动画应该正常播放。 测试
alternate 动画应该轮流反向播放。 测试

用CSS代码的方式表示,就是:
单向循环: animation-iteration-count: infinite; animation-direction: normal;
双向循环: animation-iteration-count: infinite; animation-direction: alternate;

  留几个有关css3动画写的比较好的文章:

    经验分享:多屏复杂动画CSS技巧三则

    涨姿势!CSS3动画帧数科学计算法

    【原】移动web动画设计的一点心得——css3实现跑步

  

 
上一篇: CSS3 3D变换
下一篇: CSS3—3D翻转
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287