How to implement a dynamically moving train using pure CSS
This article introduces you to the article about how to use pure CSS to realize a moving train. It has a good reference value. I hope it can help friends in need.
Effect Preview
Code Interpretation
Define dom, the container contains 2 elements, train
represents the train, track
represents the railroad track, and the 3 <span></span>
contained in it represent the 3 sleepers.
<p> </p><p></p> <p> <span></span> <span></span> <span></span> </p>
Centered display:
body{ margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(#666, #333); }
Define container size:
.loader { width: 8em; height: 10em; font-size: 20px; }
Draw the train first.
Draw the outline of the train:
.train { width: 6em; height: 6em; color: #444; background: #bbb4ab; border-radius: 1em; position: relative; left: 1em; }
Use the ::before pseudo element to draw the window:
.train::before { content: ''; position: absolute; width: 80%; height: 2.3em; background-color: currentColor; border-radius: 0.4em; top: 1.2em; left: 10%; }
Use the ::after pseudo element to draw the signal light on the window:
.train::after { content: ''; position: absolute; width: 25%; height: 0.4em; background-color: currentColor; border-radius: 0.3em; top: 0.4em; left: calc((100% - 25%) / 2); }
Use radial gradient to draw the lights:
.train { background: radial-gradient(circle at 20% 80%, currentColor 0.6em, transparent 0.6em), radial-gradient(circle at 80% 80%, currentColor 0.6em, transparent 0.6em), #bbb; }
Next draw the rails and sleepers.
Define the width of the rails, slightly wider than the train:
.track { width: 8em; }
Use pseudo elements to draw the rails:
.track { position: relative; } .track::before, .track::after { content: ''; position: absolute; width: 0.3em; height: 4em; background-color: #bbb; border-radius: 0.4em; }
Place the rails on both sides to create a visual sense of near and far. Effect:
.track::before, .track::after { transform-origin: bottom; } .track::before { left: 0; transform: skewX(-27deg); } .track::after { right: 0; transform: skewX(27deg); }
Draw the sleepers, which is the effect closest to the observer. Currently, the three sleepers are overlapping:
.track span { width: inherit; height: 0.3em; background-color: #bbb; position: absolute; top: 4em; }
Set the animation effect of the railway track:
.track span { animation: track-animate 1s linear infinite; } @keyframes track-animate { 0% { transform: translateY(-0.5em) scaleX(0.9); filter: opacity(0); } 10%, 90% { filter: opacity(1); } 100% { transform: translateY(-4em) scaleX(0.5); filter: opacity(0); } }
Set an animation delay for the other 2 sleepers to make the track look like it will never end:
.track span:nth-child(2) { animation-delay: -0.33s; } .track span:nth-child(3) { animation-delay: -0.66s; }
Finally, add an animation effect to the train to make it look like it is shaking slightly while running:
.train { animation: train-animate 1.5s infinite ease-in-out; } @keyframes train-animate { 0%, 100% { transform: rotate(0deg); } 25%, 75% { transform: rotate(0.5deg); } 50% { transform: rotate(-0.5deg); } }
Done!
Related recommendations:
How to use css to draw a bird (code)
How to use pure CSS to create a cartoon parrot Effect
The above is the detailed content of How to implement a dynamically moving train using pure CSS. For more information, please follow other related articles on the PHP Chinese website!

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.


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

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

Zend Studio 13.0.1
Powerful PHP integrated development environment
