CSS3动画:DIY Loading动画

首先要知道什么是CSS3动画?然后才能做出自己想要的动画效果。下面会通过3个简单的Loading动画效果来对CSS3 animation动画做一个简单介绍,希望对你有用。

动画是使元素从一种样式逐渐变化为另一种样式的效果。
您可以改变任意多的样式任意多的次数。
使用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%。
0% 是动画的开始,100% 是动画的完成。

要创建CSS3动画,那么首先就要了解@keyframes规则。@keyframes规则是创建动画。 @keyframes规则内指定一个CSS样式和动画将逐步从目前的样式更改为新的样式。
当在 @keyframes 创建动画,把它绑定到一个选择器,否则动画不会有任何效果。
指定至少这两个CSS3的动画属性绑定向一个选择器:规定动画的名称;规定动画的时长。


css3动画属性

Loading动画1:

点.gif

<!-- html代码 总共8个点 -->
 <div class="point-loading">
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
        <span class="point"></span>
    </div>
/*css样式代码*/
/*定义动画*/
@keyframes pointLoading {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    100% {
        transform: scale(.3);
        opacity: 0.5;
    }
}
/*定义样式*/
.point-loading {
    width: 100px;
    height: 100px;
    position: relative;
    margin: 0 auto;
    margin-top: 150px;
    margin-bottom: 100px;
}
.point-loading .point {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: lightgreen;
    position: absolute;
    animation-name:pointLoading;   /*绑定动画*/
    animation-duration:1s; /*绑定动画完成一个周期所用时间*/
    animation-iteration-count:infinite; /*动画播放次数  默认是1,infinite无限次播放*/
}
/*nth-child:选择器匹配属于其父元素的第 N 个子元素;animation-delay:动画延迟播放*/
.point-loading .point:nth-child(1) {
    left: 0;
    top: 50%;
    margin-top: -10px;
    animation-delay: 0.13s;
}
.point-loading .point:nth-child(2) {
    left: 14px;
    top: 14px;
    animation-delay: 0.26s;
}
.point-loading .point:nth-child(3) {
    left: 50%;
    top: 0;
    margin-left: -10px;
    animation-delay: 0.39s;
}
.point-loading .point:nth-child(4) {
    top: 14px;
    right: 14px;
    animation-delay: 0.52s;
}
.point-loading .point:nth-child(5) {
    right: 0;
    top: 50%;
    margin-top: -10px;
    animation-delay: 0.65s;
}
.point-loading .point:nth-child(6) {
    right: 14px;
    bottom: 14px;
    animation-delay: 0.78s;
}
.point-loading .point:nth-child(7) {
    bottom: 0;
    left: 50%;
    margin-left: -10px;
    animation-delay: 0.91s;
}
.point-loading .point:nth-child(8) {
    bottom: 14px;
    left: 14px;
    animation-delay: 1.04s;
}

Loading动画2:

圆环.gif

 <!-- html代码 -->
<div class="loading"></div>
/*css代码*/
/*首先是定义动画效果*/
@keyframes rotateLoading {
    from {
        transform:rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
/*定义基本样式,绑定动画,定义动画属性*/
.loading {
    margin: 50px auto;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    border: 10px solid  rgba(0, 0, 0, 0.2);
    border-top-color: #000;
    position: relative;
    animation-name: rotateLoading;
    animation-timing-function: linear;
    animation-duration: 1.1s;
    animation-iteration-count: infinite;
}

Loading动画3:

柱状.gif

 <!--html代码 共5个柱状 -->
 <div class="pillar-loading">
        <span class="pillar"></span>
        <span class="pillar"></span>
        <span class="pillar"></span>
        <span class="pillar"></span>
        <span class="pillar"></span>
    </div>
/*css代码*/
@keyframes pillarLoading {
    0%,
    100% {
        background: lightgreen;
    }
    50% {
        transform: scaleY(1.75);
        background: lightblue;
    }
}

.pillar-loading {
    margin: 150px auto;
    width: 60px;
    display: flex;
    justify-content: space-between;
}
.pillar-loading .pillar {
    width: 8px;
    height: 40px;
    border-radius: 4px;
    background: lightgreen;
    animation-name: pillarLoading;
    animation-iteration-count: infinite;
    animation-duration: 1s;
    animation-timing-function: ease;
}
.pillar-loading .pillar:nth-child(2){
    animation-delay: 0.2s;
}
.pillar-loading .pillar:nth-child(3){
    animation-delay: 0.4s;
}
.pillar-loading .pillar:nth-child(4){
    animation-delay: 0.6s;
}
.pillar-loading .pillar:nth-child(5){
    animation-delay: 0.8s;
}

以上3个动画是Animation动画的简单示例。
下面再说一个动画必备属性 transform。

transform 本意是变形,变换之意,在 CSS 中使用该属性可对元素进行移动(translate)、旋转(rotate)、缩放(scale)、倾斜(skew)等效果。因其有着各种特效及优良的性能,所以成为动画的标配。

** 转换方法**


transform转换方法

一个简单的小球动画,鼠标移到小球上或者空白框内,小球开始做动画,鼠标移出,动画停止。


小球动画
 <!-- html代码 -->
<div class="box">
        <div class="circle"></div>
    </div>
.box {
          width: 600px;
          height: 200px;
          border: 1px solid #ccc;
          background: #fff;
          margin: 50px,auto
}
.circle {
            width: 50px;
            height: 50px;
            background: blue;
            border-radius: 50%;
            margin: 75px,0;
            transition: all 2s   /*2s完成*/
}
.box:hover .circle {
           background: red;
           transform: translate(550px,0)   /*沿x轴偏移550px*/
}

再来一个稍微难一点的。


transform动画
 <!-- html代码 -->
<a href="https://y.qq.com/n/yqq/album/002JRl3m16wLPL.html" class="playlist-item">
        <div class="item-bd">
            <img class="item-img" src="http://coding.imweb.io/img/p3/transition-hover.jpg" alt="">
            <i class="icon-play"></i>
        </div>
        <div class="item-ft">
            <h3 class="item-tt">漂洋过海来看你 OST 原声辑</h3>
            <p class="item-author">严艺丹</p>
        </div>
    </a>
/*css样式代码*/
.playlist-item {
    display: block;
    margin-top: 100px;
    width: 300px;
    background: rgba(0, 0, 0, .6);
}
.playlist-item:hover {
    background: #31c27c;
}

.playlist-item .item-bd {
    overflow: hidden;
    position: relative;
}
.playlist-item .item-img {
    width: 100%;
    transition:all 0.75s;
}
.playlist-item .icon-play {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(.7);
    width: 70px;
    height: 70px;
    background: url(http://coding.imweb.io/img/p3/transition-cover_play.png) no-repeat;
    opacity: 0;
}
.playlist-item .item-bd:hover .item-img {
    transform:scale(1.1);
}
.playlist-item .item-bd:hover .icon-play {
    opacity: 0.8;
    transform: translate(-50%, -50%) scale(1);
}
.playlist-item .item-ft {
    color: #fff;
    padding: 15px 10px;
    text-align: center;
}
.playlist-item .item-tt {
    font-size: 16px;
    position: relative;
    display: inline-block;
    vertical-align: middle;
}
.playlist-item .item-tt::after {
    content: "...";
    width: 18px;
    height: 18px;
    font-size: 12px;
    color: #fff;
    border-radius: 50%;
    border: 2px solid #fff;
    position: absolute;
    right: -25px;
    top: 50%;
    transform: translate(0, -50%);
    line-height: 0.6;
    box-sizing: border-box;
    opacity: 0;
    transition:all 0.75s;
}
.playlist-item .item-author {
    color: #999;
}
.playlist-item:hover .item-author {
    color: #c1e9d5;
}
.playlist-item:hover .item-tt::after {
    opacity:1;
}

参考资料:CSS3 transform

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 1 CSS属性 1.1 滤镜 1.1.1 blur属性 1.1.1.1 代码示例 CSS代码: .blur { ...
    Kevin_Junbaozi阅读 688评论 1 4
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,279评论 0 11
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,689评论 0 2
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    X先生_未知数的X阅读 15,937评论 3 118
  • 一 往南热了,往北冷了,徐闻刚刚好。 如果你喜欢一望无际的大海,喜欢安静清闲的慢生活,那么,请你来徐...
    财女有钱途阅读 957评论 2 2