How to implement rail animation with css: 1. Create an HTML file and define the div as "
> ;..."; 2. Set the CSS classes to mountains and train; 3. Realize the track animation by using the animation properties of CSS3 and the animation frame "@keyframes".
How to implement car track animation in css?
Pure CSS3 cartoon train animation
By the way, I found that when I post HTML and other articles, you like them and add them to your favorites. When I post C, Java and the like, no one reads them. What do you mean?
Effect preview
##Code implementation
HTML code<div>
<div></div>
<div>
<div>
<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<div></div>
<div>
<div></div>
</div>
<div>
<div></div>
</div>
<div>
<div></div>
</div>
<div>
<div></div>
<div>
<div></div>
<div></div>
</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<div></div>
</div>
The structure of the HTML code is very clear. Generally speaking, it is divided into two parts. One part is the bright moon, mountains and starry sky as the background, and the corresponding CSS class is
mountains; the other is One part is a moving train, and the corresponding CSS class is train. The train part is relatively complicated, with elements such as a locomotive, engine, track, wheels, carriages, etc., which will be explained one by one in the following CSS code.
CSS codeThe background part draws two mountains, a bright moon and a clear starry sky.
.mountains { height: 100%; position: absolute; width: 100%; z-index: 1;}.mountains::before, .mountains::after { background: #000c18; background: -moz-linear-gradient(#012a53, #000c18 50%); background: -webkit-linear-gradient(#012a53, #000c18 50%); background: -o-linear-gradient(#012a53, #000c18 50%); background: -ms-linear-gradient(#012a53, #000c18 50%); background: linear-gradient(#012a53, #000c18 50%); content: ""; height: 100%; position: absolute;}.mountains::before { border-radius: 100% 300% 0 0; left: -20%; width: 57%;}.mountains::after { border-radius: 300% 100% 0 0; right: -20%; width: 80%;}
Then there is the bridge part of the train track, which is also very simple
.bridge { border-bottom: 0.3rem solid white; border-top: 0.3rem solid white; background: black; background: -webkit-linear-gradient(55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem, -webkit-linear-gradient(-55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem; background: -moz-linear-gradient(55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem, -moz-linear-gradient(-55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem; background: -o-linear-gradient(55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem, -o-linear-gradient(-55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem; background: -ms-linear-gradient(55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem, -ms-linear-gradient(-55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem; background: linear-gradient(55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem, linear-gradient(-55deg, transparent 46%, white 46%, white 54%, transparent 54%) left/1.6rem 2.2rem; bottom: 10rem; height: 2.6rem; position: absolute; width: 100%; z-index: 1;}
After that is the main part of this animation, the train. Each part has a corresponding CSS class to describe it. Please Look at the code:
.train { animation: move-train 24s linear infinite; bottom: 12.6rem; height: 10rem; position: absolute; left: calc(100% + 1.3rem); width: 44rem; z-index: 1;}.engine-front, .engine-front::before, .engine-front::after { background: #0f0f0f; background: linear-gradient(0deg, #0f0f0f, #262626, #0f0f0f); border: 0.1rem solid rgba(255, 255, 255, 0.15); border-radius: 50% 0 0 50%; border-right: none;}.engine-front { animation: body-upDown 0.3s infinite; border-radius: 0.5rem 0 0 0.5rem; bottom: 1.4rem; height: 4.6rem; left: 0; position: absolute; width: 8rem;}.engine-front::before, .engine-front::after { content: ""; position: absolute; height: 70%; left: -0.9rem; top: 50%; transform: translate(0, -50%); width: 0.8rem;}.engine-front::after { height: 40%; left: -1.4rem; width: 0.5rem;}.chimney { background: #990000; height: 2.2rem; left: 2.5rem; position: absolute; top: -2.3rem; width: 1.7rem;}.chimney::before, .chimney::after { content: ""; position: absolute;}.chimney::before { animation: up-down 0.3s infinite; border-bottom: none; border-left: 0.5rem solid transparent; border-right: 0.5rem solid transparent; border-top: 0.8rem solid orange; left: 50%; top: -0.9rem; transform: translate(-50%, 0); width: 160%;}.smoke { animation: move-smoke 0.4s linear infinite; background: rgba(255, 255, 255, 0.8); border-radius: 50%; height: 1.4rem; left: 0.2rem; position: absolute; top: -1.5rem; width: 0.8rem; z-index: -1;}.smoke-2 { animation-delay: 0.1s; left: 0.4rem;}.smoke-3 { animation-delay: 0.2s; left: 0.6rem;}.smoke-4 { left: 0.8rem;}.engine-body { animation: body-upDown 0.3s 0.1s infinite; background: #b30000; background: linear-gradient(0deg, #330000, red, #990000); border-radius: 0 0 0.4rem 0; bottom: 1.4rem; height: 7rem; left: 8rem; position: absolute; width: 5rem;}.engine-body::before, .engine-body::after { content: ""; left: 50%; position: absolute; transform: translate(-50%, 0);}.engine-body::before { animation: up-down 0.3s 0.2s infinite; background: #660000; background: linear-gradient(0deg, #660000, #990000, #660000); border-radius: 50% 50% 0 0; height: 1.2rem; top: -1.2rem; width: 130%;}.engine-body::after { background: #b3e0f2; border-radius: 0.2rem; height: 40%; top: 1rem; width: 45%;}.wheel::before, .wheel::after { left: 50%; top: 50%; transform: translate(-50%, -50%);}.wheel-holder { height: 2.8rem; bottom: 0; position: absolute; width: 100%;}.wheel { animation: rotate 2s linear infinite; background: orange; border: 0.3rem solid rgba(0, 0, 0, 0.6); border-radius: 50%; height: 2.8rem; left: 1rem; position: absolute; width: 2.8rem;}.wheel-2 { left: 4.5rem;}.wheel-3 { border-width: 0.5rem; height: 3.8rem; left: 8.7rem; top: -1.1rem; width: 3.8rem;}.wheel-4 { left: 15.7rem;}.wheel-5 { left: 19.5rem;}.wheel-6 { left: 25.7rem;}.wheel-7 { left: 29.5rem;}.wheel-8 { left: 35.7rem;}.wheel-9 { left: 39.5rem;}.wheel::before, .wheel::after { content: ""; position: absolute;}.wheel::before { background: rgba(0, 0, 0, 0.3); height: 86%; width: 0.2rem;}.wheel::after { background: #996300; background: -webkit-radial-gradient(circle at center, #b37400 30%, #996300 30%); background: -moz-radial-gradient(circle at center, #b37400 30%, #996300 30%); background: -o-radial-gradient(circle at center, #b37400 30%, #996300 30%); background: -ms-radial-gradient(circle at center, #b37400 30%, #996300 30%); background: radial-gradient(circle at center, #b37400 30%, #996300 30%); border-radius: 50%; height: 40%; width: 40%;}.compartment { animation: body-upDown 0.3s infinite; background: #186c8e; background: -webkit-linear-gradient(#104b63 50%, #1f8dba); background: -moz-linear-gradient(#104b63 50%, #1f8dba); background: -o-linear-gradient(#104b63 50%, #1f8dba); background: -ms-linear-gradient(#104b63 50%, #1f8dba); background: linear-gradient(#104b63 50%, #1f8dba); border-radius: 0 0 0.3rem 0.3rem; bottom: 1.4rem; height: 5rem; left: 15rem; position: absolute; width: 8rem;}.compartment-two { animation: body-upDown 0.3s 0.1s infinite; left: 25rem;}.compartment-two .compartment-window::before { animation: up-down 0.3s 0.3s infinite;}.compartment-three { animation: body-upDown 0.3s 0.2s infinite; left: 35rem;}.compartment-three .compartment-window::before { animation: up-down 0.35s infinite;}.compartment::before, .compartment::after { background: black; border: 0.4rem solid transparent; bottom: 0.4rem; box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1); content: ""; height: 0.8rem; position: absolute; width: 0.9rem;}.compartment::before { border-left: 0.4rem solid rgba(255, 255, 255, 0.3); left: -0.9rem;}.compartment::after { border-right: 0.4rem solid rgba(255, 255, 255, 0.3); left: -2rem;}.compartment-window { background: #1f8dba; background: -moz-linear-gradient(90deg, transparent 0.8rem, skyblue 0.8rem) left/2.4rem 100%; background: -webkit-linear-gradient(90deg, transparent 0.8rem, skyblue 0.8rem) left/2.4rem 100%; background: -o-linear-gradient(90deg, transparent 0.8rem, skyblue 0.8rem) left/2.4rem 100%; background: -ms-linear-gradient(90deg, transparent 0.8rem, skyblue 0.8rem) left/2.4rem 100%; background: linear-gradient(90deg, transparent 0.8rem, skyblue 0.8rem) left/2.4rem 100%; height: 40%; position: absolute; top: 0.7rem; width: 100%;}.compartment-window::before, .compartment-window::after { content: ""; position: absolute;}.compartment-window::before { animation: up-down 0.3s 0.1s infinite; background: #104b63; border-radius: 50% 50% 0 0; height: 0.6rem; left: 50%; top: -1.4rem; transform: translate(-50%, 0); width: 110%;}
So far, we have used HTML and CSS codes to draw the entire train and the surrounding environment.
The most important step is to make this little train run on the bridge. This requires the use of some animation properties of CSS3. CSS3 animation frames make a debut.
@keyframes up-down { 0%, 100% { transform: translate(-50%, 0); } 50% { transform: translate(-50%, -0.3rem); }}@keyframes rotate { 100% { transform: rotate(-360deg); }}@keyframes move-train { 100% { transform: translateX(-154rem); }}@keyframes body-upDown { 0%, 100% { transform: translateY(0); } 100% { transform: translateY(-0.2rem); }}@keyframes move-smoke { 0% { -webkit-filter: blur(0); -moz-filter: blur(0); -o-filter: blur(0); -ms-filter: blur(0); filter: blur(0); opacity: 1; } 50% { -webkit-filter: blur(0.2rem); -moz-filter: blur(0.2rem); -o-filter: blur(0.2rem); -ms-filter: blur(0.2rem); filter: blur(0.2rem); opacity: 0.6; transform: translate(0, -3rem) scale(2); } 100% { -webkit-filter: blur(0.3rem); -moz-filter: blur(0.3rem); -o-filter: blur(0.3rem); -ms-filter: blur(0.3rem); filter: blur(0.3rem); opacity: 0; transform: translate(2.5rem, -6rem) scale(3); }}@keyframes wheel-joint { 100% { transform: rotate(360deg); }}
Several things have been done in these animations:
- Let the carriage lid flap up and down to make the animated train look more realistic.
- Using filters, the steam emitted from the steam engine as the train is moving is vividly depicted.
- Let the small train run over and over the bridge as a whole, and the wheels keep turning.
- At this point, the entire animation of the train is basically completed. At the end of the article, the source code is also dedicated to everyone.
Source code download:
https://www.php.cn/link/580d3fe23a092f670c3cf2bd0231341bcss video tutorialThe above is the detailed content of How to implement car track animation with css. For more information, please follow other related articles on the PHP Chinese website!

useState()iscrucialforoptimizingReactappperformanceduetoitsimpactonre-rendersandupdates.Tooptimize:1)UseuseCallbacktomemoizefunctionsandpreventunnecessaryre-renders.2)EmployuseMemoforcachingexpensivecomputations.3)Breakstateintosmallervariablesformor

Use Context and useState to share states because they simplify state management in large React applications. 1) Reduce propdrilling, 2) The code is clearer, 3) It is easier to manage global state. However, pay attention to performance overhead and debugging complexity. The rational use of Context and optimization technology can improve the efficiency and maintainability of the application.

Using incorrect keys can cause performance issues and unexpected behavior in React applications. 1) The key is a unique identifier of the list item, helping React update the virtual DOM efficiently. 2) Using the same or non-unique key will cause list items to be reordered and component states to be lost. 3) Using stable and unique identifiers as keys can optimize performance and avoid full re-rendering. 4) Use tools such as ESLint to verify the correctness of the key. Proper use of keys ensures efficient and reliable React applications.

InReact,keysareessentialforoptimizinglistrenderingperformancebyhelpingReacttrackchangesinlistitems.1)KeysenableefficientDOMupdatesbyidentifyingadded,changed,orremoveditems.2)UsinguniqueidentifierslikedatabaseIDsaskeys,ratherthanindices,preventsissues

useState is often misused in React. 1. Misunderstand the working mechanism of useState: the status will not be updated immediately after setState. 2. Error update status: SetState in function form should be used. 3. Overuse useState: Use props if necessary. 4. Ignore the dependency array of useEffect: the dependency array needs to be updated when the state changes. 5. Performance considerations: Batch updates to states and simplified state structures can improve performance. Correct understanding and use of useState can improve code efficiency and maintainability.

Yes,ReactapplicationscanbeSEO-friendlywithproperstrategies.1)Useserver-siderendering(SSR)withtoolslikeNext.jstogeneratefullHTMLforindexing.2)Implementstaticsitegeneration(SSG)forcontent-heavysitestopre-renderpagesatbuildtime.3)Ensureuniquetitlesandme

React performance bottlenecks are mainly caused by inefficient rendering, unnecessary re-rendering and calculation of component internal heavy weight. 1) Use ReactDevTools to locate slow components and apply React.memo optimization. 2) Optimize useEffect to ensure that it only runs when necessary. 3) Use useMemo and useCallback for memory processing. 4) Split the large component into small components. 5) For big data lists, use virtual scrolling technology to optimize rendering. Through these methods, the performance of React applications can be significantly improved.

Someone might look for alternatives to React because of performance issues, learning curves, or exploring different UI development methods. 1) Vue.js is praised for its ease of integration and mild learning curve, suitable for small and large applications. 2) Angular is developed by Google and is suitable for large applications, with a powerful type system and dependency injection. 3) Svelte provides excellent performance and simplicity by compiling it into efficient JavaScript at build time, but its ecosystem is still growing. When choosing alternatives, they should be determined based on project needs, team experience and project size.


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

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.

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

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),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
