CSS3-动画

首先了解浏览器兼容问题

不同浏览器写法不同

当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。

@keyframes myfirst
{
from {background: red;}
to {background: yellow;}
}

@-moz-keyframes myfirst /* Firefox */
{
from {background: red;}
to {background: yellow;}
}

@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
from {background: red;}
to {background: yellow;}
}

@-o-keyframes myfirst /* Opera */
{
from {background: red;}
to {background: yellow;}
}

举例

<!DOCTYPE html>
<html>
<head>
<style> 
div
{
width:100px;
height:100px;
background:red;
animation:myfirst 5s;
-moz-animation:myfirst 5s; /* Firefox */
-webkit-animation:myfirst 5s; /* Safari and Chrome */
-o-animation:myfirst 5s; /* Opera */
}

@keyframes myfirst
{
from {background:red;}
to {background:yellow;}
}

@-moz-keyframes myfirst /* Firefox */
{
from {background:red;}
to {background:yellow;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {background:red;}
to {background:yellow;}
}

@-o-keyframes myfirst /* Opera */
{
from {background:red;}
to {background:yellow;}
}
</style>
</head>
<body>

<div></div>

<p><b>注释:</b>本例在 Internet Explorer 中无效。</p>

</body>
</html>

定义和用法

通过 @keyframes 规则,您能够创建动画。

创建动画的原理是,将一套 CSS 样式逐渐变化为另一套样式。

在动画过程中,您能够多次改变这套 CSS 样式。

以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。

0% 是动画的开始时间,100% 动画的结束时间。

为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。animation-timing-function

注释:请使用动画属性来控制动画的外观,同时将动画与选择器绑定。

infinite——加上之后相当于循环,一直动!

div
{
width:100px;
height:100px;
background:red;
position:relative;
animation:mymove 5s infinite;
}

animation-timing-function

规定动画的速度曲线


2262bd97638378733afe65b9efc6b783_754x243.png

CSS3-动画

当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。

通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器:

  • 规定动画的名称
  • 规定动画的时长

把 "myfirst" 动画捆绑到 div 元素,时长:5 秒:

div
{
   width:100px;
   height:100px;
   background:red;
   animation:myfirst 5s;
   -webkit-animation:myfirst 5s; /* Safari and Chrome */
@keyframes myfirst
{
  from {background:red;}
  to {background:yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */
{
  from {background:red;}
  to {background:yellow;}
}
}

注释:您必须定义动画的名称和时长。如果忽略时长,则动画不会允许,因为默认值是 0。

几个动画例子

1.当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:

div
{
  width:100px;
  height:100px;
  background:red;
  animation:myfirst 5s;
}
@keyframes myfirst
{
  0%   {background:red;}
  25%  {background:yellow;}
  50%  {background:blue;}
  100% {background:green;}
}

2.改变背景色和位置:

div
{
  width:100px;
  height:100px;
  background:red;
  position:relative;
  animation:myfirst 5s;
}@keyframes myfirst
{
  0%   {background:red; left:0px; top:0px;}
  25%  {background:yellow; left:200px; top:0px;}
  50%  {background:blue; left:200px; top:200px;}
  75%  {background:green; left:0px; top:200px;}
  100% {background:red; left:0px; top:0px;}
}

3.使 div 元素匀速向下移动:

div
{
  width:100px;
  height:100px;
  background:red;
  position:relative;
  animation:mymove 5s infinite;
}
@keyframes mymove
{
  from {top:0px;}
  to {top:200px;}
}

4.上下弹动

div
{
  width:100px;
  height:100px;
  background:red;
  position:relative;
  animation:mymove 5s infinite;
}
@keyframes mymove
{
  0%   {top:0px;}
  25%  {top:200px;}
  75%  {top:50px}
  100% {top:100px;}
}

5.在一个动画中改变多个 CSS 样式:

div
{
width:100px;
height:100px;
background:red;
position:relative;
animation:mymove 5s infinite;
}
@keyframes mymove
{
0%   {top:0px; background:red; width:100px;}
100% {top:200px; background:pink; width:600px;}
}

6.所有动画属性:

div
{
  width:100px;
  height:100px;
  background:red;
  position:relative;
  animation-name: myfirst;//规定 @keyframes 动画的名称。
  animation-duration: 5s;//规定动画完成一个周期所花费的秒或毫秒。
  animation-timing-function: linear;//规定动画的速度曲线。
  animation-delay: 2s;//规定动画何时开始。
  animation-iteration-count: infinite;//规定动画被播放的次数.
  animation-direction: alternate;//规定动画是否在下一周期逆向地播放。
  animation-play-state: running;//规定动画是否正在运行或暂停。
}
@keyframes myfirst
{
  0%   {background:red; left:0px; top:0px;}
  25%  {background:yellow; left:200px; top:0px;}
  50%  {background:blue; left:200px; top:200px;}
  75%  {background:green; left:0px; top:200px;}
  100% {background:red; left:0px; top:0px;}
}

7.简写的动画 animation 属性:

div
{
  width:100px;
  height:100px;
  background:red;
  position:relative;
  animation: myfirst 5s linear 2s infinite alternate;//顺序写上
}
@keyframes myfirst
{
  0%   {background:red; left:0px; top:0px;}
  25%  {background:yellow; left:200px; top:0px;}
  50%  {background:blue; left:200px; top:200px;}
  75%  {background:green; left:0px; top:200px;}
  100% {background:red; left:0px; top:0px;}
}

旋转

1.rotate旋转

1.gif
<style>
   .box{
        width: 150px;height: 150px;
        position: relative;
        overflow: hidden;
        background-color: pink;
    }
    .cover{
        width: 0;height: 0;
        /* background-color: orangered; */
        background: rgba(255, 69, 0, 0.6);
        position: absolute;
        transition: all 1s;
        transform: rotate(180deg);
        transform-origin: 50% 50%;
    }
    .box:hover .cover{
        width: 150px;height: 150px;
        transform: rotate(0deg);
    }
</style>
    <body>
        <div class="box">
            <div class="cover"></div>
        </div>
    </body>

2.skew

2.gif
     .box{
        width: 150px;height: 150px;
        position: relative;
        overflow: hidden;
    }
     .bg-img{
        width: 150px;height: 150px;
        background: url("images/item20.jpg") no-repeat center center;
        background-size: cover;
        
    }
      .cover{
        width: 150px;
        height: 150px;
        position: absolute;
        left: 0;top: 0;
        background: rgba(240, 128, 114, 0.35);
        transform:skew(60deg,20deg);
        /* transform: rotate(90deg); */
        transition: all 0.85s;
        transform-origin: left bottom;
        
    }
    .box:hover .cover{
        transform: rotate(0deg);
    }
    <body>
    <div class="box">
        <div class="bg-img"></div>
            <div class="cover"></div>
        </div>
    </body>

3.线条

3.gif
div{
        width: 800px;
        height: 5px;
        position: relative;
        animation: myfirst 5s infinite;
    }
    @keyframes myfirst{
        0%{
            width: 0;
            top: 0px;
        }
        25%{
            width: 25%;
            top: 200px;
            background: skyblue;
        }
        50%{
            width: 50%;
            top: 50px;
            background: salmon;
        }
        75%{
            width: 75%;
            top: 100px;
            background: orangered;
        }
        100%{
            width: 100%;
            top: 20px;
            background: teal;
        }0
    }
    <body>
         <div></div>
    </body>

4.导航

4.gif
*{margin: 0;padding: 0}
        body{
            height: 2000px;
        }
        .nav{
            height: 50px;
            background-color: salmon;
            padding: 0 10%;
            transition: all 1s;
        }
        .nav ul{
            list-style: none;
            overflow: hidden;
        }
        .nav li{
            float: left;
            margin-right: 30px;
        }.nav a{
            line-height: 100px;
        }
        .fixed{
            position: fixed;
            width: 100%;
            background: skyblue;
        }
        .fixed a{
            color: white !important; /* 强制使用 */
        }
        <body>
         <div class="nav" id="nav">
         <ul>
            <li>HOME</li>
            <li>ABOUT</li>
            <li>HOBBY</li>
            <li>CONTANCT</li>
        </ul>
        </div>
        <script>
        window.onload = function(){
            var nav =document.getElementById("nav");
            document.onscroll =function(){
                var sTop = document.documentElement.scrollTop;
                console.log(sTop);
                if(sTop>0){
                    nav.className = "nav fixed";
                }else {
                    nav.className = "nav" ;
                }
            }
        }
        </script>
        </body>

5.尖角

5.png
.box{
        width: 100px;
        height: 100px;
        background: skyblue;
        position: relative;
    }
    .box:after{
        position: absolute;
        content: "";
        border: 7px solid transparent;
        border-left-color: skyblue;
        right: -14px;top: 10px;
    }
    <body>
        <div class="box">

        </div>
    </body>

6.模态框

6.gif

利用bootstranp,先引用

<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<script src="bootstrap/js/jquery-3.3.1.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>

再直接复制代码,改东西

<body>
    <!-- Button trigger modal -->
<div class="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        Launch demo modal
      </div>
      
      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
              <img src="images/item20.jpg" alt="">
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
</body>

7.正反旋转

7.gif
    .container{
        width: 300px;height: 300px;
        margin: 100px auto;
        position: relative;
    }
    .container>div{
        width: 300px;height: 300px ;
        position: absolute;
        transition: all 3s;
        backface-visibility: hidden;
    }
    .front{
        background-color: salmon;
        z-index: 999;
        animation: toback 2s infinite;
    }
    /* .back{
        transform: rotateY(-180deg);
    } */
    /* .container:hover .front{
        transform: rotateY(180deg);
    }
    .container:hover .back{
        transform: rotateY(0deg);
    } */
    .back{
        height: 300px;
        background-color: skyblue;
        animation: tofront 2s infinite;
    }
    @keyframes toback{
        0% {transform: rotateY(0deg);}
        50% {transform: rotateY(180deg);}
        100% {transform: rotateY(360deg);}
    }
    @keyframes tofront{
        0% {transform: rotateY(-180deg);}
        50% {transform: rotateY(0deg);}
        100% {transform: rotateY(180deg);}
    }
    <body>
        <div class="container">
            <div class="front"></div>
            <div class="back"></div>
        </div>
    </body>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容