Home > Article > Web Front-end > CSS3 Animation in Practice: Super Cool Sticky Bubble Effect
This article will let you talk about CSS3 animation and see how to use pure CSS to achieve a super cool sticky bubble effect. I hope it will be helpful to everyone!
Recently, I saw such an interesting effect on CodePen:
The core difficulty of this effect lies in a special fusion effect of bubbles. [Recommended learning: css video tutorial]
The source code is at:CodePen Demo -- Goey footer, the author mainly uses SVG filters to complete If you are interested in this effect, you can check the source code.
Among them, if you want to flexibly use the feGaussianBlur
filter in SVG, you still need to have a very strong SVG knowledge reserve. So, can this effect be achieved using just CSS?
Hey, powerful CSS is certainly possible. This article will lead you step by step to use pure CSS to achieve the above effects.
First of all, if the above effect does not have the fusion effect of bubbles, it may just be like this:
It is relatively simple to create such an effect, but there will be more code. We can use the SASS preprocessor.
Suppose we have the following HTML structure:
<div class="g-wrap"> <div class="g-footer"> <div class="g-bubble"></div> <div class="g-bubble"></div> // ... 200 个 g-bubble </div> </div>
What the core needs to do is just to make 200 .g-bubble
rise up irregularly from the bottom animation.
Here, you need to use a technique -- Use animation-duration and animation-delay to build random effects.
For the same animation, we use random animation-duration
and Random animation-delay
within a certain range can effectively build a more random animation effect and make the animation more natural.
Let’s simulate it. If we use 10 circles with the same animation-duration
and animation-delay
, the core pseudo code:
<ul> <li></li> <!--共 10 个...--> <li></li> </ul>
ul { display: flex; flex-wrap: nowrap; gap: 5px; } li { background: #000; animation: move 3s infinite 1s linear; } @keyframes move { 0% { transform: translate(0, 0); } 100% { transform: translate(0, -100px); } }
In this way, the movement of the ball will be uniform like this:
To make the movement of the ball appear very random, just makeanimation-duration
and animation-delay
can float within a certain range. Transform the CSS:
@for $i from 1 to 11 { li:nth-child(#{$i}) { animation-duration: #{random(2000)/1000 + 2}s; animation-delay: #{random(1000)/1000 + 1}s; } }
We use SASS loops and random()
function, let animation-duration
be random in the range of 2-4 seconds, let animation-delay
be random in the range of 1-2 seconds, in this way, we can get very Natural and different rising animation effects, basically no repeated images, and a good simulation of random effects:
Okay, let's apply the above-mentioned techniques to the effect we want to achieve in this article, and take another look at the HTML structure :
<div class="g-wrap"> <div class="g-footer"> <div class="g-bubble"></div> <div class="g-bubble"></div> // ... 200 个 g-bubble </div> </div>
Core CSS code:
.g-footer { position: absolute; bottom: 0; left: 0; height: 86px; width: 100%; background: #26b4f5; } @for $i from 0 through 200 { .g-bubble:nth-child(#{$i}) { position: absolute; background: #26b4f5; $width: random(100) + px; left: #{(random(100)) + '%'}; top: #{(random(100))}px; width: $width; height: $width; animation: moveToTop #{(random(2500) + 1500) / 1000}s ease-in-out -#{random(5000)/1000}s infinite; } } @keyframes moveToTop { 90% { opacity: 1; } 100% { opacity: .08; transform: translate(-50%, -180px) scale(.3); } }
Here:
We use the SASS random function$width: random(100) px;
, randomly generate div circles of different sizes
Use SASS random functionleft: #{(random(100)) '%'}
, top: #{(random(100))}px
Random positioning based on the parent element
The core is animation: moveToTop #{(random (2500) 1500) / 1000}s ease-in-out -#{random(5000)/1000}s infinite
, so that the movement of all div circles is random
The combined results of the above (1) and (2) will generate such a layout, with evenly distributed circles:
注:这里为了方便理解,我隐藏了最外层g-footer
的颜色,并且给g-bubble
添加了黑色边框
接着,如果我们替换一下 animation
语句,使用统一的动画时长,去掉负的延迟,变成 animation: moveToTop 4s ease-in-out infinite
,动画就会是这样:
整体是整齐划一,没有杂乱无章的感觉的。
运用上随机效果,animation: moveToTop #{(random(2500) + 1500) / 1000}s ease-in-out -#{random(5000)/1000}s infinite
,就能得到上述的,不同气泡随机上升的感觉:
接下来,也是最重要的一步,如何让气泡与气泡之间,以及气泡和底部 .g-footer
之间产生融合效果呢?
这个技巧在此前非常多篇文章中,也频繁提及过,就是利用 filter: contrast()
滤镜与 filter: blur()
滤镜。
如果你还不了解这个技巧,可以戳我的这篇文章看看:你所不知道的 CSS 滤镜技巧与细节
简述下该技巧:
单独将两个滤镜拿出来,它们的作用分别是:
filter: blur()
: 给图像设置高斯模糊效果。
filter: contrast()
: 调整图像的对比度。
但是,当他们“合体”的时候,产生了奇妙的融合现象。
仔细看两圆相交的过程,在边与边接触的时候,会产生一种边界融合的效果,通过对比度滤镜把高斯模糊的模糊边缘给干掉,利用高斯模糊实现融合效果。
基于此,我们再简单改造下我们的 CSS 代码,所需要加的代码量非常少:
.g-wrap { background: #fff; filter: contrast(8); } .g-footer { // ... 其他保持一致 filter: blur(5px); }
就这么简单,父容器添加白色底色以及对比度滤镜 filter: contrast(8)
,子容器添加 filter: blur(5px)
即可,这样,我们就能得气泡的融合效果,基本得到我们想要的效果:
但是!利用 filter: blur()
会有一个小问题。
运用了 filter: blur()
的元素,元素边缘的模糊度不够,会导致效果在边缘失真,我们仔细看看动画的边缘:
如何解决呢?也好办,在这里,我们尝试利用 backdrop-filter
去替换 filter
。
两者之间的差异在于,filter
是作用于元素本身,而 backdrop-filter
是作用于元素背后的区域所覆盖的所有元素。
简单改造下代码,原代码:
.g-footer { // ... filter: blur(5px); }
改造后的代码:
.g-footer { // ... 去掉 filter: blur(5px) &:before { content: ""; position: absolute; top: -300px; left: 0; right: 0; bottom: 0; z-index: 1; backdrop-filter: blur(5px); } }
我们通过去到原来添加在 .g-footer
上的 filter: blur(5px)
,通过他的伪元素,叠加一层新的元素在它本身之上,并且添加了替代的 backdrop-filter: blur(5px)
。
当然,因为这里的 blur(5px)
还需要为气泡与气泡之间的融合服务,所以为了覆盖动画全区域,我们还设置了 top: -300px
,扩大了它的作用范围。
最终,我们就能完美的复刻文章一开头,使用 SVG 滤镜实现的效果:
在文章中,我省去了大部分基础的 CSS 代码,完整的代码,你可以戳这里:CodePen Demo -- Bubble Rises
(学习视频分享:web前端入门)
The above is the detailed content of CSS3 Animation in Practice: Super Cool Sticky Bubble Effect. For more information, please follow other related articles on the PHP Chinese website!