


How to use CSS and D3 to implement interactive animation of small fish swimming (with code)
The content of this article is about how to use CSS and D3 to realize interactive animation of small fish swimming (with code). It has certain reference value. Friends in need can refer to it. I hope It will help you.
Effect preview
Source code download
https://github.com/comehope/front- end-daily-challenges
Code Interpretation
Define dom, the sub-elements contained in the container represent the body, eyes, dorsal fin and tail of the fish respectively:
<div> <span></span> <span></span> <span></span> <span></span> </div>
Set page style For full screen and no scroll bars:
body { margin: 0; width: 100vw; height: 100vh; background-color: #222; overflow: hidden; }
Define the container size of the fish, --r
is a basic size unit, and all subsequent sizes are calculated based on it:
.fish { position: absolute; --r: 15vw; width: calc(var(--r) + var(--r) / 3); height: calc(var(--r) * 2); left: 50%; top: 100px; }
Draw the body of the fish, and declare the color of the fish to the parent class, because this color will be used below:
.fish { color: hsl(0, 50%, 50%); } .fish .body { position: absolute; border: var(--r) solid transparent; border-right-color: currentColor; border-left-style: none; }
Draw the eyes of the fish:
.fish .eye { position: absolute; --r1: calc(var(--r) / 4); width: var(--r1); height: var(--r1); background-color: #111; border-radius: 50%; top: 35%; left: 30%; }
Draw The dorsal fin of the fish:
.fish .fin { position: absolute; --r2: calc(var(--r) / 2); border-bottom: var(--r2) solid; border-left: var(--r2) solid transparent; filter: brightness(2.5); left: calc(var(--r) - var(--r2)); }
Draw the tail of the fish:
.fish .tail { position: absolute; --r3: calc(var(--r) / 3); border: var(--r3) solid transparent; border-right-color: currentColor; border-left-style: none; right: 0; top: calc(var(--r) - var(--r3)); }
Add the animation effect for the fish to swim, not to execute it in a loop, but only once:
.fish { right: calc(var(--r) * -1); animation: run 3s linear forwards; } @keyframes run { to { right: 100%; } }
Then add the animation effect of the swaying of the fish when swimming:
.fish { animation: run 3s linear forwards, shake 0.3s linear infinite; } @keyframes shake { 50% { transform: rotateY(-30deg); } 100% { transform: rotateY(30deg); } }
Next, set some variables to create different-looking fish:
Variables for the size of the fish--size
, the larger the value, the larger the size:
.fish { --size: 5; --r: calc(var(--size) * 1vw); }
The color variable of the fish--color
, indicating the angle of the hue circle:
.fish { --color: 0; color: hsl(var(--color), 50%, 50%); }
The fish swims from the right to the left The duration on the side, the shorter the duration, the faster the fish swims:
.fish { --duration: 3; animation: run calc(var(--duration) * 1s) linear forwards, shake 0.3s linear infinite; }
The height at which the fish appears, the larger the data, the closer it is to the lower part of the page:
.fish { --top: 100; top: calc(var(--top) * 1px); }
Next, use d3 to batch process dom elements and css variables .
Introduce the d3 library:
<script></script>
Delete the .fish
element in the html and the variable declaration code in the css file. Create a function that generates a fish. The values of css variables are randomly generated. The value range of --size
is 5 ~ 8, the value range of --color
is -60 ~ 15, - The value range of -duration
is 3 ~ 6, the value range of --top
is 100 ~ 300:
function buildFish() { let fish = d3.select('body') .append('p') .attr('class', 'fish') .style('--size', d3.randomUniform(5, 8)()) .style('--color', d3.randomUniform(-60, 15)()) .style('--duration', d3.randomUniform(3, 6)()) .style('--top', d3.randomUniform(100, 300)()); fish.append('span').attr('class', 'body'); fish.append('span').attr('class', 'eye'); fish.append('span').attr('class', 'fin'); fish.append('span').attr('class', 'tail'); }
binds the mouse click event. When the mouse is pressed A fish is generated when the It goes blank after loading:
function buildFish(e) { //略.... .style('--top', e.clientY); } window.addEventListener('click', buildFish);
Done!
Related recommendations:
How to use pure CSS to achieve the text effect of tearing tinfoil (with code)How to use pure CSS to achieve it Animation effect of an hourglass
The above is the detailed content of How to use CSS and D3 to implement interactive animation of small fish swimming (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
