前端基础入门之css动画与变形

过渡与动画

1、过渡

过渡(transition)

  • 通过过渡可以指定一个属性发生变化时的切换方式
  • 通过过渡可以创建一些非常好的效果,提升用户的体验

属性值

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

  • 多个属性间使用,隔开;
  • 如果所有属性都需要过渡,则使用all关键字;
  • 大部分属性都支持过渡效果;
  • 注意过渡时必须是从一个有效数值向另外一个有效数值进行过渡;

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

  • 时间单位:s 和 ms(1s=1000ms)

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

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

  • linear匀速运动
  • ease 默认值,慢速开始,先加速后减速
  • ease-in 加速运动
  • ease-out 减速运动
  • ease-in-out 先加速后减速
  • cubic-bezier()来指定时序函数 https://cubic-bezier.com
  • steps()分步执行过渡效果,可以设置第二个值:
    • end,在时间结束时执行过渡(默认值)
    • start,在时间开始时执行过渡

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

  • 只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟时间

示例

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

transition

几种过渡效果对比

linear匀速运动

transition-timing-function: linear;

linear

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

transition-timing-function: ease;

ease

ease-in 加速运动

transition-timing-function: ease-in;

ease-in

ease-out 减速运动

transition-timing-function: ease-out;

ease-out

ease-in-out 先加速后减速

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

ease-in-out

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

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

cubic-bezier

steps()分步执行过渡效果

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

steps-end

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

steps-start

2、动画

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

  • 过渡需要在某个属性发生变化时才会触发
  • 动画可以自动触发动态效果

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

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

  to {
    
    
    margin-left: 900px;
  }
}

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

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

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

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

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

  • infinite 无限执行

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

  • normalfromto运行,每次都是这样,默认值
  • reversetofrom运行,每次都是这样
  • alternatefromto运行,重复执行动画时反向执行
  • alternate-reversetofrom运行,重复执行动画时反向执行

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

  • running 动画执行,默认值
  • paused 动画暂停

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

  • none 动画执行完毕,元素回到原来位置,默认值
  • forwards 动画执行完毕,元素会停止在动画结束的位置
  • backwards 动画延时等待时,元素就会处于开始位置
  • both 结合了forwardsbackwards

示例

/* 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、实战

米兔

.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;
}

米兔

奔跑的少年

.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;
  }
}

奔跑的少年

弹力球

.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;
  }
}

弹力球

酷炫球

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;
  }
}

酷炫球

变形:平移、旋转与缩放

变形就是指通过 css 来改变元素的形状或位置

变形不会影响到页面的布局

transform用来设置元素的变形效果

4、平移

  • translateX() 沿着由方向平移
  • translateY() 沿着 y 轴方向平移
  • translateZ() 沿着 z 轴方向平移平移元素

百分比是相对于自身计算的

几种水平垂直双方向居中的方式对比

  1. 绝对定位的方式

    /* 这种居中方式,只适用于元素的大小确定 */
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    
  2. table-cell的方式

    /* table-cell的方式具有一定局限性 */
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    
  3. transform的方式

    /* transform变形平移的方式 */
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    

浮出效果

div {
    
    
  float: left;
  width: 200px;
  height: 300px;
  background-color: silver;
  margin: 100px 50px auto 50px;
  transition: all 0.3s;
}

div:hover {
    
    
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  transform: translateY(-5px);
}

浮出效果

5、Z 轴平移

z 轴平移,调整元素在 z 轴的位置,正常情况就是调整元素和人眼之间的距离,距离越大,元素离人越近

z 轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果必须要设置网页的视距

透视效果

html {
    
    
  background-color: rgb(71, 44, 32);
  perspective: 800px;
}

.box {
    
    
  width: 200px;
  height: 300px;
  background-color: silver;
  margin: 100px auto;
  transition: all 0.3s;
}

.box:hover {
    
    
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  transform: translateZ(200px);
}

透视效果

6、旋转

通过旋转可以使元素沿着 x、y 或 z 旋转指定的角度

  • rotateX()
  • rotateY()
  • rotateZ()
/* transform: rotateY(0.5turn); */
transform: rotateY(180deg);

旋转效果

7、缩放

对元素进行缩放的函数

  • scalex() 水平方向缩放
  • scaleY() 垂直方向缩放
  • scale() 双方向的缩放
.box {
    
    
  height: 200px;
  width: 200px;
  background-color: #bfa;
  margin: 200px auto;
  transition: 2s;
}

.box:hover {
    
    
  /* transform: scaleX(2); */
  /* transform: scaleY(2); */
  transform: scale(2);
  /* 变形的原点 */
  transform-origin: 0 0;
}

变形原点

8、实战

鸭子表

html 代码

<div class="clock">
  <div class="hour-wrapper">
    <div class="hour"></div>
  </div>
  <div class="minute-wrapper">
    <div class="minute"></div>
  </div>
  <div class="second-wrapper">
    <div class="second"></div>
  </div>
</div>

css 代码

.clock {
    
    
  width: 500px;
  height: 500px;
  background-image: url("assets/鸭子表/clock.png");
  background-image: url("assets/鸭子表/clock_duck.jpg");
  background-size: cover;
  margin: 100px auto;
  position: relative;
}

.clock > div {
    
    
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

.clock > div > div {
    
    
  height: 50%;
  margin: 0 auto;
}

/* 时针 */
.hour-wrapper {
    
    
  height: 60%;
  width: 60%;
  animation: clock-run 720s infinite;
}

.hour {
    
    
  width: 8px;
  background-color: black;
}

/* 分针 */
.minute-wrapper {
    
    
  height: 75%;
  width: 75%;
  animation: clock-run 60s steps(60) infinite;
}

.minute {
    
    
  width: 4px;
  background-color: black;
}

/* 秒针 */
.second-wrapper {
    
    
  height: 90%;
  width: 90%;
  animation: clock-run 1s steps(60) infinite;
}

.second {
    
    
  width: 2px;
  background-color: red;
}

@keyframes clock-run {
    
    
  from {
    
    
    transform: rotateZ(0);
  }

  to {
    
    
    transform: rotateZ(360deg);
  }
}

鸭子表

复仇者联盟

html 代码

<div class="cube">
  <div class="surface1"></div>
  <div class="surface2"></div>
  <div class="surface3"></div>
  <div class="surface4"></div>
  <div class="surface5"></div>
  <div class="surface6"></div>
</div>

css 代码

html {
    
    
  perspective: 800px;
}

.cube {
    
    
  height: 200px;
  width: 200px;
  margin: 200px auto;
  position: relative;
  /* 设置3d变形效果 */
  transform-style: preserve-3d;
  animation: cube-rotate 12s infinite linear;
}

.cube div {
    
    
  height: 200px;
  width: 200px;
  background-size: cover;
  position: absolute;
  top: 0;
  left: 0;
  /* 为元素设置透明效果 */
  opacity: 0.85;
}

.surface1 {
    
    
  background-image: url("/assets/复仇者联盟/1.jpg");
  transform: translateX(-100px) rotateY(90deg);
}

.surface2 {
    
    
  background-image: url("/assets/复仇者联盟/2.jpg");
  transform: translateX(100px) rotateY(90deg);
}

.surface3 {
    
    
  background-image: url("/assets/复仇者联盟/3.jpg");
  transform: translateY(-100px) rotateX(90deg);
}

.surface4 {
    
    
  background-image: url("/assets/复仇者联盟/4.jpg");
  transform: translateY(100px) rotateX(90deg);
}

.surface5 {
    
    
  background-image: url("/assets/复仇者联盟/5.jpg");
  transform: translateZ(-100px);
}

.surface6 {
    
    
  background-image: url("/assets/复仇者联盟/6.jpg");
  transform: translateZ(100px);
}

@keyframes cube-rotate {
    
    
  from {
    
    
    transform: rotateX(0) rotateY(0) rotateZ(0);
  }

  to {
    
    
    transform: rotateX(1turn) rotateY(2turn) rotateZ(3turn);
  }
}

复仇者联盟

猜你喜欢

转载自blog.csdn.net/agonie201218/article/details/125382728