# CSS 动画 transition 与 animation

制作 CSS 动画最常使用transitionanimation.因为它们的性能开销小,功能强大.但是不是说记住了所有的属性名和作用就能写出漂亮的 CSS 动画.而是要具有想象力.这篇博文以小见大,分别使用transitionanimation实现一个跳动的红心.

# transition

HTML 部分:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JS Bin</title>
  </head>
  <div>
    <div class="heart">
      <div class="left"></div>
      <div class="right"></div>
      <div class="bottom"></div>
    </div>
  </div>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

CSS 部分:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.heart {
  position: relative;
  margin: 100px;
  display: inline-block;
  transition: all 1s;
}
.heart:hover {
  transform: scale(1.2, 1.2);
}
.heart > .left {
  background: red;
  width: 100px;
  height: 100px;
  border-radius: 50% 0 0 50%;
  position: absolute;
  bottom: 100%;
  right: 100%;
  transform: rotate(45deg) translateX(80px);
}
.heart > .right {
  background: red;
  width: 100px;
  height: 100px;
  border-radius: 50% 50% 0 0;
  position: absolute;
  bottom: 100%;
  left: 100%;
  transform: rotate(45deg) translateY(80px);
}
.heart > .bottom {
  background: red;
  width: 100px;
  height: 100px;
  transform: rotate(45deg);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# animation

HTML 部分不变, 以下是CSS部分:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.heart {
  position: relative;
  margin: 100px;
  display: inline-block;
  transition: all 1s;
  animation: heartjump 1s infinite alternate;
}

@keyframes heartjump {
  0% {
    transform: scale(1, 1);
  }
  100% {
    transform: scale(1.2, 1.2);
  }
}
.heart > .left {
  background: red;
  width: 100px;
  height: 100px;
  border-radius: 50% 0 0 50%;
  position: absolute;
  bottom: 100%;
  right: 100%;
  transform: rotate(45deg) translateX(80px);
}
.heart > .right {
  background: red;
  width: 100px;
  height: 100px;
  border-radius: 50% 50% 0 0;
  position: absolute;
  bottom: 100%;
  left: 100%;
  transform: rotate(45deg) translateY(80px);
}
.heart > .bottom {
  background: red;
  width: 100px;
  height: 100px;
  transform: rotate(45deg);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 效果链接