


How to use pure CSS to implement block jumping animation (source code attached)
The content of this article is about how to use pure CSS to realize block jumping animation (source code attached). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Effect preview
Source code download
https://github.com/comehope/front- end-daily-challenges
Code Interpretation
Define dom, the container contains 2 sub-elements, representing 1 girl and a group of boys (4), eachspan
Element represents 1 person (1 square):
<figure> <span></span> <p> <span></span> <span></span> <span></span> <span></span> </p> </figure>
Centered display:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; }
Define container size and layout of its child elements:
.container { width: 8em; height: 1em; font-size: 35px; display: flex; justify-content: space-between; }
Draw 5 squares , use the border as an auxiliary line to help positioning:
.container span { width: 1em; height: 1em; border: 1px dashed black; /* 辅助线 */ } .boys { width: 6em; display: flex; justify-content: space-between; }
Use pseudo elements to style the elements to make them softer, fill in different colors for boys and girls, and delete the auxiliary lines from the previous step:
.container span::before { content: ''; position: absolute; width: inherit; height: inherit; border-radius: 15%; box-shadow: 0 0 0.2em rgba(0, 0, 0, 0.3); } .girl::before { background-color: hotpink; } .boys span::before { background-color: dodgerblue; }
Make the colors of the four boys’ color blocks gradually lighter and add a little layering:
.boys span:nth-child(1)::before { filter: brightness(1); } .boys span:nth-child(2)::before { filter: brightness(1.15); } .boys span:nth-child(3)::before { filter: brightness(1.3); } .boys span:nth-child(4)::before { filter: brightness(1.45); }
Next, create the animation effect.
First add the effect of the girl's movement, and also fade the color. The timing of other animations must be consistent, so set the animation duration as a variable:
.container span { width: 1em; height: 1em; --duration: 3s; } .girl { animation: slide var(--duration) ease-in-out infinite; } @keyframes slide { from { transform: translateX(0); filter: brightness(1); } to { transform: translatex(calc(8em - (1em * 1.25))); filter: brightness(1.45); } }
Then add the first one The animation effect of the boy jumping away. Note that the origin of the rotation from 15% to 35% is directly above the element:
.boys span { animation: var(--duration) ease-in-out infinite; } .boys span:nth-child(1) { animation-name: jump-off-1; } @keyframes jump-off-1 { 0%, 15% { transform: rotate(0deg); } 35%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } }
Refer to the animation effect of the first boy, and add the animation of the other 3 boys jumping away. The difference is that the key frame time is adjusted and delayed by 15%:
.boys span:nth-child(2) { animation-name: jump-off-2; } .boys span:nth-child(3) { animation-name: jump-off-3; } .boys span:nth-child(4) { animation-name: jump-off-4; } @keyframes jump-off-2 { 0%, 30% { transform: rotate(0deg); } 50%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } } @keyframes jump-off-3 { 0%, 45% { transform: rotate(0deg); } 65%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } } @keyframes jump-off-4 { 0%, 60% { transform: rotate(0deg); } 80%, 100% { transform-origin: -50% center; transform: rotate(-180deg); } }
Adds an anthropomorphic animation effect to the first boy. This effect is written in ::before
In the pseudo element, the animation process is from normal to squashed, then stretched, then squashed, and finally returned to normal. Pay attention to the squashed deformation from 25% to 40%, because the main element has been turned over at this time, so## The origin of #transform-origin is different from the origin of the squashing deformation from 5% to 15%:
.boys span::before { animation: var(--duration) ease-in-out infinite; } .boys span:nth-child(1)::before { filter: brightness(1); animation-name: jump-down-1; } @keyframes jump-down-1 { 5% { transform: scale(1, 1); } 15% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 20%, 25% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 40% { transform-origin: center top; transform: scale(1.3, 0.7); } 55%, 100% { transform: scale(1, 1); } }
::before pseudo-element Effect, add another 3 anthropomorphic animation effects of boys, the only difference is that the time of the key frames is adjusted, and the time is delayed by 15%:
.boys span:nth-child(2)::before { animation-name: jump-down-2; } .boys span:nth-child(3)::before { animation-name: jump-down-3; } .boys span:nth-child(4)::before { animation-name: jump-down-4; } @keyframes jump-down-2 { 20% { transform: scale(1, 1); } 30% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 35%, 40% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 55% { transform-origin: center top; transform: scale(1.3, 0.7); } 70%, 100% { transform: scale(1, 1); } } @keyframes jump-down-3 { 35% { transform: scale(1, 1); } 45% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 50%, 55% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 70% { transform-origin: center top; transform: scale(1.3, 0.7); } 85%, 100% { transform: scale(1, 1); } } @keyframes jump-down-4 { 50% { transform: scale(1, 1); } 60% { transform-origin: center bottom; transform: scale(1.3, 0.7); } 65%, 70% { transform-origin: center bottom; transform: scale(0.8, 1.4); } 85% { transform-origin: center top; transform: scale(1.3, 0.7); } 100%, 100% { transform: scale(1, 1); } }
Add
alternate parameters to all animation attributes so that all animations are executed back and forth to achieve the effect of returning from the right side to the left side:
.girl { animation: slide var(--duration) ease-in-out infinite alternate; } .boys span { animation: var(--duration) ease-in-out infinite alternate; } .boys span::before { animation: var(--duration) ease-in-out infinite alternate; }
How to use pure CSS to achieve the animation effect of a ring rotation illusion (source code attached)
How to use pure CSS CSS realizes the display frame effect of butterfly specimens
The above is the detailed content of How to use pure CSS to implement block jumping animation (source code attached). 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.

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.

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

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.

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.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

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