找回密码
 立即注册

css3动画animation

[复制链接]
admin 发表于 2025-5-27 09:18:24 | 显示全部楼层 |阅读模式
css3动画animation
2.jpg

1.jpg

  1. <title>动画实现步骤</title>
  2.     <style>
  3.       .box {
  4.         width: 200px;
  5.         height: 100px;
  6.         background-color: pink;
  7.         
  8.         animation: change 1s;
  9.       }

  10.       /* 动画一:宽度从200变化到800 */
  11.       /* @keyframes change {
  12.         from {
  13.             width: 200px;
  14.         }
  15.         to {
  16.             width: 800px;
  17.         }
  18.       } */


  19.       /* 动画二:从 200*100 变化到 300*300 变化到800*500 */
  20.       /* 百分比:表示的意思是动画时长的百分比 */
  21.       @keyframes change {
  22.         0% {
  23.             width: 200px;
  24.             height: 100px;
  25.         }
  26.         20% {
  27.             width: 300px;
  28.             height: 300px;
  29.         }
  30.         100% {
  31.             width: 800px;
  32.             height: 500px;
  33.         }
  34.       }

  35.     </style>
  36.   </head>
  37.   <body>
  38.     <div class="box"></div>
  39.   </body>
复制代码
  1. <title>animation复合属性</title>
  2.     <style>
  3.       .box {
  4.         width: 200px;
  5.         height: 100px;
  6.         background-color: pink;

  7.         /* linear:匀速 */
  8.         animation: change 1s linear;

  9.         /* steps:分步动画,工作中,配合精灵图实现精灵动画 */
  10.         animation: change 1s steps(3);

  11.         /* 如果有两个时间,第一个是动画时长,第二个是延迟时间 */
  12.         animation: change 1s 2s;

  13.         /* 重复次数,infinite:无限循环 */
  14.         animation: change 1s 3;
  15.         animation: change 1s infinite;

  16.         /* alternate:反向 */
  17.         animation: change 1s infinite alternate;

  18.         /* 动画执行完毕时的状态, forwards:结束状态; backwards:开始状态(默认) */
  19.         animation: change 1s forwards;
  20.         animation: change 1s backwards;
  21.       }

  22.       /* 宽度 从 200 变化到 800 */
  23.       @keyframes change {
  24.         from {
  25.             width: 200px;
  26.         }
  27.         to {
  28.             width: 800px;
  29.         }
  30.       }

  31.     </style>
  32.   </head>
  33.   <body>
  34.     <div class="box"></div>
  35.   </body>
复制代码
 楼主| admin 发表于 2025-5-28 08:12:42 | 显示全部楼层
1.jpg
2.jpg
  1. <title>animation拆分写法</title>
  2.     <style>
  3.       .box {
  4.         width: 200px;
  5.         height: 100px;
  6.         background-color: pink;
  7.         
  8.         /* 动画名称 */
  9.         animation-name: change;
  10.         /* 动画时长 */
  11.         animation-duration: 1s;
  12.         /* 播放次数 */
  13.         animation-iteration-count: infinite;

  14.         /* animation-play-state: paused; */
  15.       }

  16.       .box:hover {
  17.         /* 暂停动画 */
  18.         animation-play-state: paused;
  19.       }


  20.       /* 宽度从 200 变化到 800 */
  21.       @keyframes change {
  22.         0% {
  23.             width: 200px;
  24.         }
  25.         100% {
  26.             width: 800px;
  27.         }
  28.       }
  29.     </style>
  30.   </head>
  31.   <body>
  32.     <div class="box"></div>
  33.   </body>
复制代码

1.jpg

  1. <title>走马灯</title>
  2.     <style>
  3.       * {
  4.         padding: 0;
  5.         margin: 0;
  6.       }
  7.       li {
  8.         list-style: none;
  9.       }

  10.       img {
  11.         display: block;
  12.         width: 200px;
  13.       }
  14.       
  15.       .box {
  16.         width: 600px;
  17.         height: 112px;
  18.         border: 5px solid #000;
  19.         margin: 100px auto;
  20.         overflow: hidden;
  21.       }

  22.       .box ul {
  23.         display: flex;
  24.         animation: move 6s infinite linear;
  25.       }

  26.       /* 定义位移动画;ul使用动画;鼠标悬停暂停动画 */
  27.       @keyframes move {
  28.         0% {
  29.           transform: translate(0);
  30.         }
  31.         100% {
  32.           transform: translate(-1400px);
  33.         }
  34.       }

  35.       .box:hover ul {
  36.         animation-play-state: paused;
  37.       }
  38.     </style>
  39.   </head>
  40.   <body>
  41.     <div class="box">
  42.       <ul>
  43.         <li><img src="./images/1.jpg" alt="" /></li>
  44.         <li><img src="./images/2.jpg" alt="" /></li>
  45.         <li><img src="./images/3.jpg" alt="" /></li>
  46.         <li><img src="./images/4.jpg" alt="" /></li>
  47.         <li><img src="./images/5.jpg" alt="" /></li>
  48.         <li><img src="./images/6.jpg" alt="" /></li>
  49.         <li><img src="./images/7.jpg" alt="" /></li>
  50.         <!-- 替身:填补显示区域的露白 -->
  51.         <li><img src="./images/1.jpg" alt="" /></li>
  52.         <li><img src="./images/2.jpg" alt="" /></li>
  53.         <li><img src="./images/3.jpg" alt="" /></li>
  54.       </ul>
  55.     </div>
  56.   </body>
复制代码
1.jpg
  1. <title>精灵动画</title>
  2.   <style>
  3.     div {
  4.       width: 140px;
  5.       height: 140px;
  6.       border: 1px solid #000;
  7.       background-image: url(./images/bg.png);
  8.       animation: run 1s steps(12) infinite;
  9.     }

  10.     @keyframes run {
  11.       from {
  12.         background-position: 0 0;
  13.       }
  14.       to {
  15.         background-position: -1680px 0;
  16.       }
  17.     }
  18.   </style>
  19. </head>
  20. <body>
  21.   <div></div>
  22. </body>
复制代码
1.jpg

  1. <title>多组动画</title>
  2.   <style>
  3.     div {
  4.       width: 140px;
  5.       height: 140px;
  6.       /* border: 1px solid #000; */
  7.       background-image: url(./images/bg.png);
  8.       animation:
  9.         run 1s steps(12) infinite,
  10.         move 3s forwards
  11.       ;
  12.     }

  13.     /* 当动画的开始状态样式 跟 盒子默认样式相同,可以省略动画开始状态的代码 */
  14.     @keyframes run {
  15.       /* from {
  16.         background-position: 0 0;
  17.       } */
  18.       to {
  19.         background-position: -1680px 0;
  20.       }
  21.     }

  22.     @keyframes move {
  23.       /* 0% {
  24.         transform: translate(0);
  25.       } */
  26.       100% {
  27.         transform: translate(800px);
  28.       }
  29.     }
  30.   </style>
  31. </head>
  32. <body>
  33.   <div></div>
  34. </body>
复制代码
1.jpg
  <title>全民出游季</title>
  <link rel="stylesheet" href="./css/index.css">
</head>
<body>
  <!-- -->
  <div class="cloud">
    <img src="./images/yun1.png" alt="">
    <img src="./images/yun2.png" alt="">
    <img src="./images/yun3.png" alt="">
  </div>

  <!-- 文字 -->
  <div class="text">
    <img src="./images/font1.png" alt="">
  </div>
</body>
  1. * {
  2.   margin: 0;
  3.   padding: 0;
  4. }

  5. /* 大背景 */
  6. /* 默认状态HTML和body的高度是0,所以导致cover缩放背景图不成功 */
  7. html {
  8.   height: 100%;
  9. }
  10. body {
  11.   height: 100%;
  12.   background: url(../images/f1_1.jpg) no-repeat center 0 / cover;
  13.   /* background-size: cover; */
  14. }

  15. /* 云 */
  16. .cloud img {
  17.   position: absolute;
  18.   left: 50%;
  19. }

  20. .cloud img:nth-child(1) {
  21.   margin-left: -250px;
  22.   top: 20px;
  23.   animation: cloud 1s infinite alternate linear;
  24. }
  25. .cloud img:nth-child(2) {
  26.   margin-left: 400px;
  27.   top: 100px;
  28.   animation: cloud 1s infinite alternate linear 0.4s;
  29. }
  30. .cloud img:nth-child(3) {
  31.   margin-left: -550px;
  32.   top: 200px;
  33.   animation: cloud 1s infinite alternate linear 0.6s;
  34. }

  35. @keyframes cloud {
  36.   100% {
  37.     transform: translate(20px);
  38.   }
  39. }

  40. /* 文字 */
  41. .text img {
  42.   position: absolute;
  43.   left: 50%;
  44.   top: 50%;
  45.   transform: translate(-50%, -50%);
  46.   animation: text 1s;
  47. }

  48. /* 默认 → 小 → 大 → 小 → 默认 */
  49. @keyframes text {
  50.   0% {
  51.     transform: translate(-50%, -50%) scale(1);
  52.   }
  53.   20% {
  54.     transform: translate(-50%, -50%) scale(0.1);
  55.   }
  56.   40% {
  57.     transform: translate(-50%, -50%) scale(1.4);
  58.   }
  59.   70% {
  60.     transform: translate(-50%, -50%) scale(0.8);
  61.   }
  62.   100% {
  63.     transform: translate(-50%, -50%) scale(1);
  64.   }
  65. }
复制代码




QQ|网站地图|Archiver|手机版|金黑网络 ( 粤ICP备2021124338号 )

网站建设,微信公众号小程序制作,商城系统开发,高端系统定制,app软件开发,智能物联网开发,直播带货系统等

Powered by Www.Jinhei.Cn

Copyright © 2013-2024 深圳市金黑网络技术有限公司 版权所有

快速回复 返回顶部 返回列表