Lesson 11 · 11 steps
animation_css.html and six stylesheets: things that flash, move, walk a square, spin and arrive from off-screen. Every one of them is CSS — there is no JavaScript anywhere in this lesson.
/* 2) блимання кольором: найпростіша анімація — два стани */
div.p3 {animation-duration:3s; animation-name:flash_color; animation-iteration-count:infinite}
@keyframes flash_color {
from { background:#3a2a1a; color:#f0a500 }
to { background:#f0a500; color:#141414 }
}
<div class="warn fadeInLeft"> Ласкаво просимо на CS2 UA </div> <!-- 2) блимання кольором --> <h2> Блимання кольором </h2> <div class="p3" style="width:420px;padding:10px;font-size:16pt"> Бомба закладена: 40 секунд </div>
An animation is two things: @keyframes describing what changes, and a rule attaching it to an element. from and to are the start and the end; the browser fills in everything between them.
animation-duration is how long one pass takes, animation-iteration-count:infinite means it never stops.
/* 3) блимання разом із рамкою */
div.p4 {animation-duration:4s; animation-name:block_flashing; animation-iteration-count:infinite}
@keyframes block_flashing {
from { background:#2b1d1d; border:solid #ff7b5b 6px }
to { background:#1d2b1d; border:solid #8bd450 6px }
}
<!-- 3) блимання разом із рамкою --> <h2> Блимання рамкою </h2> <div class="p4" style="width:420px;padding:10px;color:#e6e6e6;font-size:16pt"> Останній раунд половини </div>
Any number of properties can change in one animation. Not all of them animate smoothly, though: colours and sizes do, but something like display simply switches at the halfway mark.
/* 4) рух: кур'єр доставки бомби їде зліва направо */
div.p5 {animation-duration:4s; animation-name:box; animation-iteration-count:infinite}
@keyframes box {
from { left:10px }
to { left:320px }
}
<!-- 4) рух --> <h2> Рух </h2> <div class="lane"> <div class="p5" style="position:absolute;top:0;width:300px;height:60px;background:#2b2b2b;border:solid #f0a500 6px;padding:5px;color:#f0a500;font-size:14pt"> Кур'єр несе бомбу на точку </div> </div>
Animating left works and is the expensive way to do it: the browser re-lays out the page on every frame. transform, further down, moves the block without touching the layout at all.
Which is why the usual advice is to animate only transform and opacity. Everything else can move too, at the cost of frames dropped — visible on a phone, invisible on a laptop, so it goes unnoticed until someone complains.
/* 5) колір і рух одночасно */
div.p6 {animation-duration:5s; animation-name:move_and_color; animation-iteration-count:infinite}
@keyframes move_and_color {
from { left:10px; background:#3a2a1a; color:#f0a500 }
to { left:300px; background:#1d2b1d; color:#8bd450 }
}
<!-- 5) рух і колір одночасно --> <h2> Рух і колір разом </h2> <div class="lane"> <div class="p6" style="position:absolute;top:0;width:300px;height:60px;border:solid #888 6px;padding:5px;font-size:14pt"> Захід через мід </div> </div>
One keyframe set changing three properties. There is no need to run three animations in parallel for something like this.
/* 7) рух по точках: відсотки замість from/to */
div.p7 {animation-duration:3s; animation-name:moving; animation-iteration-count:infinite}
@keyframes moving {
0% { transform: translate(0, 0) }
30% { transform: translate(60px, 0) }
60% { transform: translate(60px, 60px) }
100% { transform: translate(0, 60px) }
}
<!-- 7) рух по точках --> <h2> Рух по точках </h2> <div class="lane"> <div class="p7" style="width:120px;height:40px;background:#3a2a1a;border:solid #f0a500 4px;padding:5px;color:#f0a500"> обхід точки </div> </div>
Percentages replace from and to when the motion has corners: 0, 30, 60 and 100 per cent of the duration, and the block walks a square.
translate(x, y) moves an element from where it sits, without disturbing anything around it.
/* 8) обертання */
div.p8 {animation-duration:4s; animation-name:spin; animation-iteration-count:infinite}
@keyframes spin {
from { transform: rotate(0) }
to { transform: rotate(1turn) }
}
<!-- 8) обертання --> <h2> Обертання </h2> <div class="p8" style="width:90px;height:90px;border:solid #8bd450 6px;background:#2b2b2b"></div>
rotate(1turn) is one full revolution. Angles can be written in degrees, turns or radians — 360deg, 1turn and 6.28rad all mean the same.
@keyframes fadeInLeft {
from { opacity:0; transform:translateX(-600px) }
to { opacity:1; transform:none }
}
/* 11) обертання малюнка, яке відбувається один раз */
.wheel {animation:wheel 3s 1s; animation-fill-mode:both}
<a href="main.html"> головна сторінка </a>
Two properties animate at once: opacity from 0 to 1 and transform from far off-screen to nothing. No iteration-count, so it runs once, when the page opens.
This is where a quiet rule applies: anyone who has asked their system to reduce motion should not get any of this. The site you are reading does exactly that — the background animation stops under @media (prefers-reduced-motion: reduce).
from { transform: translateX(0) rotate(0) }
to { transform: translateX(380px) rotate(2turn) }
}
/* місце для блоків, які рухаються */
<!-- 11) обертання малюнка --> <h2> Малюнок, який котиться </h2>
Two transforms in one line: translateX carries the crosshair across while rotate(2turn) spins it twice. The shorthand animation: wheel 3s 1s is name, duration and a one-second delay.
animation-fill-mode:both is what keeps it in place at the end — without it the picture would snap back to the start the moment the animation finished.