# 泡泡特效

源码 (opens new window) html部分

  <section>
    <h2>Bubbles</h2>
  </section>
1
2
3

css

* {
  margin: 0;
  padding: 0;
}

section {
  position: relative;
  overflow: hidden;
  background: #111;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
section h2 {
  font-size: 10em;
  color: #333;
}

section span {
  position: absolute;
  bottom: -50px;
  background: transparent;
  border-radius: 50%;
  pointer-events: none;
  box-shadow: inset 0 0 10px rgba(255, 255, 255, 0.5);
  animation: animate 4s linear infinite;
}

/* 中间白色小圆点制作 */
section span::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  transform: scale(0.25) translate(-70%, -70%);
  background: radial-gradient(#fff, transparent);
  border-radius: 50%;
}

@keyframes animate {
  0% {
    transform: translateY(0%);
    opacity: 1;
  }
  99% {
    opacity: 1;
  }
  100% {
    transform: translateY(-1200%);
    opacity: 0;
  }
}

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
48
49
50
51
52
53
54
55

js

    const section = document.querySelector('section');
    function createBubbles () {
      //创建气泡元素
      const newBubble = document.createElement('span');
      //自定义气泡大小随机数
      let size = Math.random() * 60;
      newBubble.style.width = 20 + size + 'px'
      newBubble.style.height = 20 + size + 'px';
      newBubble.style.left = Math.random() * innerWidth + 'px';
      section.appendChild(newBubble);
      setTimeout(() => {
        //每4s后新生成的气泡销毁
        newBubble.remove();
      }, 4000)
    }
    setInterval(createBubbles, 50);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
最后更新时间: 5/19/2023, 2:42:49 PM