惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
博客园_首页
T
Tailwind CSS Blog
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
S
SegmentFault 最新的问题
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
Jina AI
Jina AI
WordPress大学
WordPress大学
U
Unit 42
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog

CloudCannon Blog

Building with AI: Git-based vs headless vs traditional CMS CloudCannon + Astro: performance meets powerful content management Introducing the Astro Component Starter Introducing Jetstream — built on the Astro Component Starter Why we switched to the system font stack Redesigning CloudCannon’s docs with Diátaxis, Lume, and Pagefind Make content editing more visual: upgraded Editable Regions How Configuration Mode makes building editing interfaces easy Your hosting just got an upgrade (and a price cut) Custom testing domains for professional branding Keep your content consistent with input validation Managing multilingual content in CloudCannon Simplify team publishing with conflict resolution and domain tools Open Beta: Publishing Conflict Resolution Getting started with CloudCannon and Astro: Bookshop, components, and live editing Welcome to the CloudCannon Community! Omnichannel delivery is just marketing spin from API-based CMS companies Getting started with CloudCannon and Astro: Snippets and Collections Managing digital assets in CloudCannon: a guide to smart asset storage Understanding CloudCannon's branching workflows and Projects: a complete guide What is a static website? CloudCannon’s 2024 wrapped Getting started with CloudCannon and Astro: WYSIWYG blogging Jamstack vs. WordPress: reasons to make the change The top five static site generators for 2025 (and when to use them!) Free Jekyll themes for 2025: ten great community options Eleventy (11ty) vs. Hugo How to set up WYSIWYG editing with MkDocs Material The rise of static-first websites: why major brands are making the switch Watching your Core Web Vitals on Jamstack
How to create an animated pickup in CSS and HTML
2014-12-19 · via CloudCannon Blog

This week I came across the 7mml website and I loved the loaded symbol. An animated car with a scrolling background. It was rather sluggish so I looked at how it was built. It was actually built using a large background image with JavaScript changing the background position.

What is it? Direct link to this section

I am going to rebuild the car/pickup in HTML, SVG and CSS. I was going to build it all in CSS but as it is a complicated shape SVG is probably better. Here's my version:

illustration of white truck used for animating

illustration of white truck frame used for animating

<div class="truck">
<div class="body">
<img src="truck-under.svg">
<div class="shine"></div>
<img src="truck-frame.svg" class="frame">
</div>

<div class="speed-thingy"></div>
<div class="speed-thingy second"></div>
<div class="shadow"></div>

<div class="wheel back"></div>
<div class="wheel front"></div>
</div>

Let's break it down.

The Wheels Direct link to this section

I started building the car using just HTML and CSS so theses are circles built using border-radius. I used the :before and :after pseudo-elements too for the inner circles. You may be asking "Why did you not use a border on the inner circle?" - if I used border I would not be able to use percentage widths. Then I added an infinite spin animation which works well because of the slightly inconsistent size;

illustration of white truck frame used for animating

@keyframes spin {
0% { transform: rotate(0); }
100% { transform: rotate(360deg); }
}

.wheel,
.wheel:after,
.wheel:before
{
bottom: -20%;
width: 12.8%;
height: 46.38%;
border-radius: 100%;
background: #1e2327;
}

.wheel {
left: 21.5%;
-webkit-animation: spin 0.4s infinite linear;
animation: spin 0.4s infinite linear;
}

.wheel.front {
left: 82%;
}

.wheel:after,
.wheel:before
{
content: "";
position: absolute;
display: block;
top: 20%;
left: 20%;
width: 60%;
height: 60%;
}

.wheel:before {
background: #aaa;
}

.wheel:after {
top: 40%;
left: 40%;
width: 20%;
height: 20%;
}

The Body Direct link to this section

I started building the body with HTML elements and CSS but felt I was using the wrong tool for the job. I decided to use SVG instead. There are actually two assets to be exact and I overlay one on the other with a slight rotation and vertical transition to give it a bit of character.

illustration of white truck used for animating

illustration of white truck frame used for animating

@keyframes bobbing {
0%{
transform: rotate(0) translateY(0);
}
100%{
transform: rotate(0.1deg) translateY(5px);
}
}

.body {
width: 100%;
position: relative;
transform-origin: right center;
animation: bobbing 0.2s infinite ease-in-out forwards alternate;
}

.frame {
position: relative;
}

.truck img {
width: 100%;
height: auto;
top: 0;
left: 0;
margin: 0;
}

The Shine Direct link to this section

This uses the same effect as the Facebook content placeholder. It has the bobble animation as it's placed inside the .body of the truck. The first image is to show the gradient and the shape which is achieved using transforms and a linear gradient. The second one has an animation applied.

@keyframes shine {
0%{
opacity: 0.1;
background-position: 50px top;
}
30% { opacity: 1; }
50%, 100%{
opacity: 0.2;
background-position: -500px 0;
}
}

.shine {
/* Position it in the gap */
width: 67%;
height: 37%;
left: 9%;
top: 6%;

/* Shape of the div to fit the windows */
transform: skew(33deg, 1deg);
border-radius: 56px 100px 20px 0;

/* The background */
background: linear-gradient(to right, transparent 76%, rgba(255,255,255,0.8) 78%, rgba(255,255,255,0.8) 96%, transparent 98%);
background-repeat: no-repeat;
background-size: cover;

/* The animation */
animation: shine 3s infinite ease forwards;
}

The Speed Things Direct link to this section

These are simple shrinking and growing divs.

illustration of white truck frame used for animating

@keyframes thingy {
0%{
transform: translateX(0);
width: 0;
opacity: 0;
}
10%{
width: 10%;
opacity: 1;
}
30%, 100%{
transform: translateX(-250px);
width: 0;
opacity: 0;
}
}

.speed-thingy {
position: absolute;
width: 20px;
border-radius: 10px;
height: 5%;
background: #fff;
top: -10%;
left: 50%;
animation: thingy 3s infinite ease forwards;
}

.speed-thingy.second {
top: 50%;
left: 30%;
animation-delay: 0.1s;
}

A Shadow to top it all off Direct link to this section

This makes the bobbing effect seem more realistic and shows a platform for the wheels to sit on. We want it to stay still at the front so we change the transform origin to the front. Once that's done we scale it ever so slightly with timing to match the body.

<img src="https://cc-dam.imgix.net/blog/assets/blog/assets/blog/truck-frame.svg"alt="illustration of white truck frame used for animating" width="500" height="136" class="frame">

@keyframes shadow {
0%{
transform: scale(1);
}
100%{
transform: scale(1.01);
}
}

.shadow {
width: 90%;
border-radius: 100%;
height: 20px;
bottom: -25%;
left: 5%;
background: rgba(0,0,0,0.1);
animation: shadow 0.2s infinite ease-in-out forwards alternate;
transform-origin: right center;
}

All Together Again Direct link to this section

illustration of white truck used for animating

<img src="/assets/blog/truck-frame.svg"alt="illustration of white truck frame used for animating" width="500" height="136" class="frame">

Why would I ever use this? Direct link to this section

Using CSS animations rather than sprite sheets will give you:

  • A crisp look on all screen resolutions
  • Seamless tweening between states.
  • Flexibility to reuse work (It's much easier to change code then recreate assets)

That's it Direct link to this section

I have altered the original quite a bit and the key with these animations was subtlety. Each part adds up to a make it look like it is traveling forward even without the background animations. Going back to Tim's one word idea, for animations it would be to "subtlety" or "reactive". Animations should not surprise the user, they should feel natural and help convey a real world metaphor. If an animation is off continued use will annoy the user not improve their experience. Hope you enjoyed this deconstruction, comment below if you have any questions.