Свойство CSS `offset-rotate`
Базовая поддержка Широко доступно
Эта функция хорошо зарекомендовала себя и работает на многих устройствах и в разных версиях браузеров. Она доступна во всех браузерах с сентября 2022 года.
Свойство offset-rotate CSS определяет ориентацию/направление элемента при его перемещении вдоль offset-path.
Попробуйте
offset-rotate: auto;
offset-rotate: 90deg;
offset-rotate: auto 90deg;
offset-rotate: reverse;
<section class="default-example" id="default-example"> <div class="transition-all" id="example-element"></div> <button id="playback" type="button">Play</button> </section>
#example-element {
width: 24px;
height: 24px;
background: #2bc4a2;
offset-path: path("M-70,-40 C-70,70 70,70 70,-40");
animation: distance 8000ms infinite linear;
animation-play-state: paused;
clip-path: polygon(0% 0%, 70% 0%, 100% 50%, 70% 100%, 0% 100%, 30% 50%);
}
#example-element.running {
animation-play-state: running;
}
#playback {
position: absolute;
top: 0;
left: 0;
font-size: 1em;
}
@keyframes distance {
0% {
offset-distance: 0%;
}
100% {
offset-distance: 100%;
}
}
/* Provides a reference image of what path the element is following */
#default-example {
position: relative;
background-position: calc(50% - 12px) calc(50% + 14px);
background-repeat: no-repeat;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="-75 -45 150 140" width="150" height="140"><path d="M-70,-40 C-70,70 70,70 70,-40" fill="none" stroke="lightgrey" stroke-width="2" stroke-dasharray="4.5"/></svg>');
}
const example = document.getElementById("example-element");
const button = document.getElementById("playback");
button.addEventListener("click", () => {
if (example.classList.contains("running")) {
example.classList.remove("running");
button.textContent = "Play";
} else {
example.classList.add("running");
button.textContent = "Pause";
}
});
Примечание: В ранних версиях спецификации это свойство называлось motion-rotation.
Синтаксис
/* Follow the path direction, with optional additional angle */ offset-rotate: auto; offset-rotate: auto 45deg; /* Follow the path direction but facing the opposite direction of `auto` */ offset-rotate: reverse; /* Keep a constant rotation regardless the position on the path */ offset-rotate: 90deg; offset-rotate: 0.5turn; /* Global values */ offset-rotate: inherit; offset-rotate: initial; offset-rotate: revert; offset-rotate: revert-layer; offset-rotate: unset;
-
auto - Элемент поворачивается на угол, соответствующий направлению
offset-pathотносительно положительной полуоси x. Это значение по умолчанию. -
<angle> - К элементу применяется постоянное преобразование поворота по часовой стрелке на заданный угол.
-
auto <angle> - Если за
autoследует<angle>, вычисленное значение угла прибавляется к вычисленному значениюauto. -
reverse - Элемент поворачивается аналогично
auto, но обращён в противоположную сторону. Это эквивалентно указанию значенияauto 180deg.
Формальное определение
| Начальное значение | auto |
|---|---|
| Применяется к | трансформируемым элементам |
| Наследуется | нет |
| Вычисленное значение | как задано |
| Тип анимации | как <angle>, <basic-shape> или <path()> |
Формальный синтаксис
offset-rotate =
[ auto | reverse ] ||
<angle>
Примеры
Ориентация элемента вдоль траектории смещения
HTML
<div></div> <div></div> <div></div>
CSS
div {
width: 40px;
height: 40px;
background: #2bc4a2;
margin: 20px;
clip-path: polygon(0% 0%, 70% 0%, 100% 50%, 70% 100%, 0% 100%, 30% 50%);
animation: move 5000ms infinite alternate ease-in-out;
offset-path: path("M20,20 C20,50 180,-10 180,20");
}
div:nth-child(1) {
offset-rotate: auto;
}
div:nth-child(2) {
offset-rotate: auto 90deg;
}
div:nth-child(3) {
offset-rotate: 30deg;
}
@keyframes move {
100% {
offset-distance: 100%;
}
}
Результат
Спецификации
Совместимость с браузерами
| Компьютеры | Мобильные устройства | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome для Android | Firefox для Android | Opera для Android | Safari для iOS | Samsung Internet | WebView для Android | WebView для iOS | |
offset-rotate |
56
55
46
|
79
79
79
|
72 |
43
42
33
|
16 |
56
55
46
|
79 |
43
42
33
|
16 |
6.0
6.0
5.0
|
56
55
46
|
16 |
auto |
46 |
79 |
72 |
33 |
16 |
46 |
79 |
33 |
16 |
5.0 |
46 |
16 |
reverse |
46 |
79 |
72 |
33 |
16 |
46 |
79 |
33 |
16 |
5.0 |
46 |
16 |
См. также
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/offset-rotate