美文网首页
CSS3笔记---动画功能

CSS3笔记---动画功能

作者: 珍珠林 | 来源:发表于2016-11-07 22:53 被阅读0次

1. Transition动画

Transition功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画功能。(IE11以下支持的不好)

示例:鼠标移动到div上后,1秒内,将div的背景从黄色变成浅蓝色

div{
    background-color: #ffff00; // 黄色
    // 背景色,在1秒内,以linear的方式变化。延迟2秒后执行。
    transition: background-color 1s linear 2s; // 第4个值可选
    // 等同于以下属性
    // transition-property: background-color;
    // transition-duration: 1s;
    // transition-timing-function: linear;
    // transition-delay: 2s;
}
div:hover{
    background-color: #00ffff; // 浅蓝色
}

transition功能也可以同时平滑过渡多个属性值:
示例:1秒内,同时变化背景色,字体颜色和宽度

div{
    background-color: #ffff00;
    color: #000000;
    width: 300px;
    transition: background-color 1s linear, color 1s linear, width 1s linear;
}
div:hover{
    background-color: #003366;
    color: #ffffff;
    width: 400px;
}

示例:右移30像素并顺时针旋转180度

img{
    position: absolulte;
    top: 70px;
    left: 0;
    transform: rotate(0deg);
    transition: left 1s linear, transform 1s linear;
}
div:hover img{
    position: absolute;
    left: 30px;
    transform: rotate(180deg);
}

2. Animation动画

Transition功能实现动画的缺点是只能让属性从开始值和终点值之间平滑过渡,不能实现更为复杂的效果。在CSS3中,还可以使用Animation功能,以定义关键帧的方式,实现更为复杂的动画效果。(IE11以下支持的不好)

将div从红色变成深蓝色,从深蓝色变成黄色,从黄色变回红色:

div{
    background-color: red;
}
// 定义关键帧集合,命名为mycolor
@keyframes mycolor{
    // 定义起始关键帧样式
    0%{
        background-color: red;
    }
    // 定义动画过程40%处的关键帧样式
    40%{
        background-color: darkblue;
    }
    70%{
        background-color: yellow;
    }
    100%{
        background-color: red;
    }
}
div:hover{
    animation: mycolor 5s linear;
    // 等同于如下
    // animation-name: mycolor;
    // animation-duration: 5s;
    // animation-timing-function: linear;
    // animation-delay: 2s // 或2000ms
    // 还可以指定动画循环次数
    // animation-iteration-count: 1; // infinite(无限次)
}

Animation也可以实现多个属性值同时改变的动画
示例:同时改变颜色和角度

div{
    position: absolute;
    background-color: yellow;
    top: 100px;
    width: 500px;
}
@keyframes mycolor{
    0%{
        background-color: red;
        transform: rotate(0deg);
    }
    40%{
        background-color: darkblue;
        transform: rotate(30deg);
    }
    70%{
        background-color: yellow;
        transform: rotate(-30deg);
    }
    100%{
        background-color: red;
        transform: rotate(0deg);
    }
}
div:hover{
    // animation: mycolor 5s linear;
    animation-name: mycolor;
    animation-duration: 5s;
    animation-timing-function: linear;
    // 还可以指定动画循环次数
    animation-iteration-count: infinite;
}

去除鼠标悬停的样式,并把此样式直接作用在div上,则动画将在页面打开时进行播放:

@keyframes mycolor{
    0%{
        background-color: red;
        transform: rotate(0deg);
    }
    40%{
        background-color: darkblue;
        transform: rotate(30deg);
    }
    70%{
        background-color: yellow;
        transform: rotate(-30deg);
    }
    100%{
        background-color: red;
        transform: rotate(0deg);
    }
}
div{
    animation-name: mycolor;
    animation-duration: 5s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}

示例:实现网页的淡入效果

@keyframes fadein{
    0%{
        opacity: 0;
        background-color: white;
    }
    100%{
        opacity: 1;
        background-color: white;
    }
}
body{
    animation-name: fadein;
    animation-duration: 5s;
    animation-timing-function: linear;
    animation-iteration-count: 1;
}

3. 实现动画的方式

在前面的几个动画示例中,我们只使用一种实现动画的方式------linear。除了linear外,还有几种实现动画的方式:

方式 属性值的变化速度
linear 在动画开始时与结束时以同样的速度进行改变
ease-in 动画开始时速度很慢,然后速度沿曲线值进行加快
ease-out 动画开始时速度很快,然后速度沿曲线值进行放慢
ease 动画开始时速度很慢,然后速度沿曲线值进行加快,然后再沿曲线值进行放慢
ease-in-out 动画开始时速度很慢,然后速度沿曲线值进行加快,然后再沿曲线值进行放慢

相关文章

  • 08_dayCSS动画

    CSS3新增的功能有:过渡和动画,阴影和圆角 css3过渡动画: css3都有哪些新增的东西 : 过度,动画,阴影...

  • CSS3动画

    动画是CSS3中新增功能,CSS3中动画分为两种,分别是transitions和animations,transi...

  • 网页高级知识点(三)

    CSS3 transition动画 CSS3 transform变换 CSS3 animation动画

  • CSS3笔记---动画功能

    1. Transition动画 Transition功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另...

  • 07day

    CSS3动画 CSS3 @keyframes 规则如需在 CSS3 中创建动画,您需要学习 @keyframes ...

  • css3之Transform

    要说css3最受大家欢迎的新功能,无外乎动画、变形、媒体查询和弹性布局。今天来说说css3的变形—transfor...

  • CSS3中的动画功能

    在CSS3中主要依靠transitions和animations实现动画功能 Transtions 主要用法tra...

  • CSS3 动画

    CSS3 动画CSS3,我们可以创建动画,它可以取代许多网页动画图像,Flash动画,和JAVAScripts。 ...

  • CSS3 动画

    动画属性 动画(animation)是CSS3新增的属性,动画属性可以为元素创建动画,CSS3以前在网页上实现动画...

  • css3动画

    css3动画 CSS3 可以创建动画,它可以取代许多网页动画图像、Flash 动画和 JavaScript 实现的...

网友评论

      本文标题:CSS3笔记---动画功能

      本文链接:https://www.haomeiwen.com/subject/tyrguttx.html