CSS3动画之DIY Loading动画

所属分类: 网页制作 / CSS 阅读数: 1903
收藏 0 赞 0 分享

首先要知道什么是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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

CSS配合JavaScript做酷的动态页面效果

  利用CSS配合JavaScript的可以做很多更酷的动态页面效果,在本教程的最后给大家简单介绍一下CSS配合JS的应用。首先,要搞清楚事件和动作的概念。在客户端脚本中,JavaScript 通过对事件进行响应来获得与用户的交互。例如,当用户单击一个按钮或者在某段文字上移动鼠标
收藏 0 赞 0 分享

WEB标准,Web前端开发工程师必备技术列表

  想要打造并拥有一流的Web产品开发团队,在团队成员基础能力上一定要下功夫。对于Web前端产品开发来说,仅仅掌握Web1.0时代简单的"网页套接"是完全不够的。我结合自己的团队配备,特此罗列了Web前端产品工程师所涉及的技能列表如下:   通过许多实际项目,
收藏 0 赞 0 分享

用CSS制作Alpha滤镜测试板

alpha滤镜给制作网页特效提供了较大的创作空间,但由于它控制参数较多,在实际应用时,为了确定一组合适的参数值,不得不反复调整修改,在编辑窗口和预览窗口来回倒腾,甚是麻烦,本文介绍了一种简单的方法。制作一个“Alpha滤镜参数测试板”,在测试板上输入参数
收藏 0 赞 0 分享

非常流行的所谓的气泡窗口

普通的Alt无法自定义风格,而Sweet Titles通过JS脚本与CSS的集合.自定义了这种伪Alt风格. 前一段时间非常流行的,就所谓的气泡窗口(鼠标移到链接处出现的). 我们这里实现的用的是Sweet Titles的插件.显示效果完全由CSS控制.. 先下载Sweet Ti
收藏 0 赞 0 分享

CSS教程:li和ul标签用法举例

LI代码的格式化: A).运用CSS格式化列表符: ul li{ list-style-type:none; } B).如果你想将列表符换成图像,则: ul li{ list-style-type:none; list-style-image: url(/blog/images/
收藏 0 赞 0 分享

CSS教程:CSS中的定位(position)

  使用CSS来定位页面内层的位置,一直是比较难以掌握的事情,很多时候,往往被绝对定位的元素,总是以浏览器的左上角为坐标原点,此时,如果浏览器的大小改变,被定义的层就会偏离设计想要的位置,让人很挠头。   其实,要想控制好层的绝对定位,只要理解CSS中关于定位
收藏 0 赞 0 分享

CSS教程:盒模型(BOX Model)

  如果想熟练掌握DIV和CSS的布局方法,首先要对盒模型有足够的了解。每个HTML元素都可以看作一个装了东西的盒子,盒子里面的内容到盒子的边框之间的距离即填充(padding),盒子本身有边框(border),而盒子边框外和其他盒子之间,还有边界(margin),如图1所示。
收藏 0 赞 0 分享

无延迟翻滚的图形与CSS混合风格按钮

  在一个具有图形背景的按钮中添加CSS风格的文本,这种建立按钮的方法结合了具有CSS翻滚(CSS rollover)标记的开发速度和效率,从而有效地提高按钮外表图像的三维效果。   相比于常规的图形按钮,这些图形/CSS混合按钮可易于建立和载入,因为你只需要为空白按钮外面
收藏 0 赞 0 分享

css里expression实现界面对象的批量控制

用过css样式我们就知道, 可以定义一批对象的class属性来指定同一个样式来统一界面. 但如何统一同类型的对象的事件? 比如:界面有无数个 <img src="**.jpg"> 如何实现鼠标经过此图片, 图片的src变成是**_over.jpg?
收藏 0 赞 0 分享

CSS教程:水平对齐(text-align)

  水平对齐(text-align),用以设定元素内文本的水平对齐方式。   1.语法   text-align具体参数如下: 语法:text-align:left|right|center|justify 说明:设定元素内文本的水平对齐方式。 参数:left:左
收藏 0 赞 0 分享
查看更多