search
HomeWeb Front-endCSS TutorialA brief discussion on the idea of ​​cleverly using CSS to create wave effects

This article will give you an idea of ​​how to skillfully use CSS to create a wave effect. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

This article will introduce another wave effect achieved using CSS. The idea is very interesting.

Start with the area of ​​a curved triangle using definite integrals

Before entering the topic, let’s take a look at this. In advanced mathematics, we can use definite integrals to find the area of ​​a triangle. The area of ​​a subfunction curved edge graph.

We can divide the area under the curve into n thin and tall rectangles. When n approaches infinity, the area of ​​all rectangles is equal to the area of ​​the curved edge figure.

Two simple schematic diagrams, the pictures are taken from Why can definite integrals be used to find the area?

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

When n infinitely approaches infinity, the area of ​​all rectangles is equal to the area of ​​the curved edge figure:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Using this idea, we can also simulate a curved edge, that is, a wavy line, in CSS through multiple divs.

Step 1. Cut the graphic into multiple parts

First, we can define a parent container with 12 child divs under the parent container:

<div class="g-container">
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
  <div class="g-item"></div>
</div>

Through flex layout, simply lay out and get such a graphic, with each sub-element having the same height:

.g-container {
    width: 200px;
    height: 200px;
    border: 2px solid #fff;
    display: flex;
    align-items: flex-end;
}

.g-item {
    flex-grow: 1;
    height: 60px;
    background-color: #fff;
}

The effect is as follows:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Step 2. Let each child element run the height transformation animation with different negative delays

Next, with a simple transformation, we need to make this image move by changing the height of each child element Implementation:

.g-item {
    flex-grow: 1;
    height: 60px;
    background-color: #000;
    animation: heightChange 1s infinite ease-in-out alternate;
}

@keyframes heightChange {
    from {
        height: 60px;
    }
    to {
        height: 90px;
    }
}

The effect is as follows:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Next, you only need to set a negative delay of different times for the animation sequence of each child element. , you can get a preliminary wave effect. In order to reduce the workload here, we use SASS to implement:

$count: 12;
$speed: 1s;

.g-item {
    --f: #{$speed / -12};
    flex-grow: 1;
    height: 60px;
    background-color: #000;
    animation: heightChange $speed infinite ease-in-out alternate;
}

@for $i from 0 to $count {
    .g-item:nth-child(#{$i + 1}) {
        animation-delay: calc(var(--f) * #{$i});
    }
}

@keyframes heightChange {
    from {
        height: 60px;
    }
    to {
        height: 90px;
    }
}

In this way, we get a preliminary wave effect:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Step 3. Eliminate aliasing

As you can see, there are certain aliases in the above wave animation. What we have to do next is to eliminate these as much as possible. Sawtooth.

Method 1: Increase the number of divs

According to the idea of ​​using definite integrals to find the area of ​​curved edge graphics at the beginning, we only need to increase the number of sub-divs as much as possible, that is However, when the number of divs is infinite, the jagged edges will disappear.

We can try to replace the above 12 sub-divs with 120. It is too laborious to write 120 divs one by one. Here we use the Pug template engine:

div.g-container
 -for(var i=0; i<120; i++)
    div.g-item

For the CSS code, you only need to change the animation delay time. The negative delay of the 120 sub-divs is controlled within 1s:

// 12 -- 120
$count: 120;
$speed: 1s;

.g-item {
    // 注意,只有这里发生了变化
    --f: #{$speed / -120};
    flex-grow: 1;
    height: 60px;
    background-color: #000;
    animation: heightChange $speed infinite ease-in-out alternate;
}

@for $i from 0 to $count {
    .g-item:nth-child(#{$i + 1}) {
        animation-delay: calc(var(--f) * #{$i});
    }
}

In this way, we can get a smoother curve:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Method 2: Use transform: skew() to simulate radians

Of course, in actual situations, using so many divs is too much It's a waste, so is there any other way to eliminate aliasing as much as possible when the number of divs is relatively small?

Here, we can try to add different transform: skewY() to the child elements during the motion transformation process to simulate radians.

Let’s transform the code again. We will reduce the number of divs and add an animation effect of transform: skewY() to each sub-div:

div.g-container
 -for(var i=0; i<24; i++)
    div.g-item

Complete The CSS code is as follows:

$count: 24;
$speed: 1s;

.g-item {
    // 注意,只有这里发生了变化
    --f: #{$speed / -24};
    flex-grow: 1;
    height: 60px;
    background-color: #000;
    animation: 
        heightChange $speed infinite ease-in-out alternate,
        skewChange $speed infinite ease-in-out alternate;
}

@for $i from 0 to $count {
    .g-item:nth-child(#{$i + 1}) {
        animation-delay: 
            calc(var(--f) * #{$i}), 
            calc(var(--f) * #{$i} - #{$speed / 2});
    }
}

@keyframes heightChange {
    from {
        height: var(--h);
    }
    to {
        height: calc(var(--h) + 30px);
    }
}

@keyframes skewChange {
    from {
        transform: skewY(20deg);
    }
    to {
        transform: skewY(-20deg);
    }
}

In order to facilitate understanding, first look at the transformation of the child div with skewY() added when the height transformation animation is consistent:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

You can see that each transformation has obvious protruding aliasing. Superimposing the delayed height transformation can effectively eliminate most of the aliasing effect:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

At this point, we have another method of anti-aliasing with a moderate number of divs! For the complete code of all the above effects, you can click here:

CodePen -- PureCSS Wave Effects

Mixed use

Finally, we can adjust several variable parameters to combine several different The wave effects are combined together to get some combination effects, which is also very good.

Something like this:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

CodePen -- PureCSS Wave Effects 2

Based on this, I think we The LOGO of Sea Group, the parent company of the company (Shopee), looks like this:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

Use the solution in this article to implement a dynamic LOGO animation for it:

A brief discussion on the idea of ​​cleverly using CSS to create wave effects

CodePen Demo -- PureCSS Wave - Sea Group Logo

Disadvantages

This solution The shortcomings are still very obvious:

  • First of all, it is useless div, which requires more divs to achieve the effect, and the more divs, the better the effect will be. Of course, if it is increased to a certain level, lag will not be possible. The avoided
  • jaggies cannot be completely eliminated. This is the most fatal or affects the place where it can really be useful.

Of course, the purpose of this article is more to explore. Thinking, discuss the advantages and disadvantages of this method, the entire process of realizing animation, and the use of animation negative delay time, all have some reference and learning significance. CSS is still very interesting~

Original address: https://segmentfault.com/a/1190000040017751

Author: chokcoco

More programming For related knowledge, please visit: programming video! !

The above is the detailed content of A brief discussion on the idea of ​​cleverly using CSS to create wave effects. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use