其他分享
首页 > 其他分享> > 【Web前端HTML5&CSS3】12-过渡与动画

【Web前端HTML5&CSS3】12-过渡与动画

作者:互联网

过渡与动画

1、过渡

过渡(transition)

属性值

transition-property:指定要执行过渡的属性

transition-duration:指定过渡效果的持续时间

transition-delay:过渡效果的延迟,等待一段时间后在执行过渡

transition-timing-function:过渡的时序函数

transition:可以同时设置过渡相关的所有属性

示例

CSS
/* transition: margin-left 2s 1s; */
transition-property: margin-left;
transition-duration: 2s;
transition-delay: 1s;

transition

几种过渡效果对比

linear匀速运动

CSS
transition-timing-function: linear;

linear

ease 默认值,慢速开始,先加速后减速

CSS
transition-timing-function: ease;

ease

ease-in 加速运动

CSS
transition-timing-function: ease-in;

ease-in

ease-out 减速运动

CSS
transition-timing-function: ease-out;

ease-out

ease-in-out 先加速后减速

CSS
transition-timing-function: ease-in-out;

ease-in-out

cubic-bezier()来指定时序函数

CSS
transition-timing-function: cubic-bezier(0.17, 1.79, 0.68, -0.69);

cubic-bezier

steps()分步执行过渡效果

CSS
/* transition-timing-function: steps(2, end); */
transition-timing-function: steps(2);

steps-end

CSS
transition-timing-function: steps(2, start);

steps-start

2、动画

动画和过渡类似,都是可以实现一些动态的效果,不同的是

设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤

CSS
@keyframes test {
  from {
    margin-left: 0;
  }

  to {
    margin-left: 900px;
  }
}

animation-name 指定动画的关键帧名称

animation-duration:指定动画效果的持续时间

animation-delay:动画效果的延迟,等待一段时间后在执行动画

animation-timing-function:动画的时序函数

animation-iteration-count 动画执行的次数

animation-direction 指定动画运行的方向

animation-play-state 设置动画的执行状态

animation-fill-mode 动画的填充模式

示例

CSS
/* animation-name: test;
animation-duration: 2s;
animation-delay: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both; */

animation: test 2s 2s linear infinite alternate both;

animation

3、实战

米兔

CSS
.box {
  height: 271px;
  width: 132px;
  background-image: url("/assets/米兔/bigtap-mitu-queue-big.png");
  margin: 100px auto;
  transition: background-position 1s steps(4);
}

.box:hover {
  background-position: -528px 0;
}

米兔

奔跑的少年

CSS
.box {
  height: 256px;
  width: calc(1536px / 6);
  background-image: url("/assets/奔跑的少年/bg2.png");
  margin: 100px auto;
  animation: run 1s steps(6) infinite;
}

/* 关键帧 */
@keyframes run {
  from {
    background-position: 0 0;
  }

  to {
    background-position: -1536px 0;
  }
}

奔跑的少年

弹力球

CSS
.outer {
  width: 100%;
  height: 700px;
  border-bottom: 10px solid #000;
  /* 外边距重叠,开启BFC */
  overflow: hidden;
}

.ball {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: gray;
  animation: bounce 6s ease-in;
}

@keyframes bounce {
  from {
    margin-top: 0;
  }

  5%,
  15%,
  25%,
  35%,
  45%,
  55%,
  65%,
  75%,
  85%,
  95%,
  98%,
  to {
    margin-top: 600px;
    animation-timing-function: ease-out;
  }

  10%,
  20%,
  30%,
  40%,
  50%,
  60%,
  70%,
  80%,
  90% {
    animation-timing-function: ease-in;
  }

  10% {
    margin-top: 60px;
  }

  20% {
    margin-top: 120px;
  }

  30% {
    margin-top: 180px;
  }

  40% {
    margin-top: 240px;
  }

  50% {
    margin-top: 300px;
  }

  60% {
    margin-top: 360px;
  }

  70% {
    margin-top: 420px;
  }

  80% {
    margin-top: 480px;
  }

  90% {
    margin-top: 540px;
  }

  96% {
    margin-top: 580px;
  }

  99% {
    margin-top: 590px;
  }
}

弹力球

酷炫球

CSS
div {
  float: left;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  animation: bounce 0.5s infinite ease-in alternate;
}

.ball1 {
  background-color: red;
  animation-delay: 0.1s;
}

.ball2 {
  background-color: yellow;
  animation-delay: 0.2s;
}

.ball3 {
  background-color: green;
  animation-delay: 0.3s;
}

.ball4 {
  background-color: blue;
  animation-delay: 0.4s;
}

.ball5 {
  background-color: pink;
  animation-delay: 0.5s;
}

.ball6 {
  background-color: orange;
  animation-delay: 0.6s;
}

.ball7 {
  background-color: fuchsia;
  animation-delay: 0.7s;
}

.ball8 {
  background-color: gray;
  animation-delay: 0.8s;
}

.ball9 {
  background-color: darkcyan;
  animation-delay: 0.9s;
}

.ball10 {
  background-color: indigo;
  animation-delay: 1s;
}

.ball11 {
  background-color: black;
  animation-delay: 1.1s;
}

.ball12 {
  background-color: darkcyan;
  animation-delay: 1.2s;
}

.ball13 {
  background-color: darkkhaki;
  animation-delay: 1.3s;
}

.ball14 {
  background-color: brown;
  animation-delay: 1.4s;
}

.ball15 {
  background-color: mediumpurple;
  animation-delay: 1.5s;
}

@keyframes bounce {
  from {
    margin-top: 0;
  }

  to {
    margin-top: 500px;
  }
}

酷炫球

标签:CSS3,Web,12,动画,transition,delay,animation,background,margin
来源: https://www.cnblogs.com/https-blog-paofu-net/p/16293674.html