Вы можете просто добавить оболочку вокруг элементов, у которых уже есть свойство animation
, и вместо этого повернуть эту оболочку:
const watch = document.getElementById('watch');
const changeTimeButton = document.getElementById('changeTimeButton');
let shift = 0;
changeTimeButton.onclick = (date) => {
shift = (shift + 360 / 12) % 360;
watch.style.transform = `rotate(${ shift }deg)`;
};
@keyframes hours {
to {
transform: rotate(360deg);
}
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
#changeTimeButton {
margin-top: 16px;
}
#watch {
position: relative;
box-shadow: 0 0 32px 0 rgba(0, 0, 0, .125);
border-radius: 100%;
width: 50vh;
height: 50vh;
}
.hours-hand {
position: absolute;
top: 8px;
bottom: 50%;
left: calc(50% - 2px);
width: 4px;
background: #232425;
transform-origin: 50% 100%;
animation-name: hours;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
<div id="watch">
<div class="hours-hand"></div>
</div>
<button id="changeTimeButton">Change Time</button>