Как сделать анимирование при нажатии на кнопку?
Помогите пожалуйста с css анимированием как сделать так что бы анимирование воспроизводилось при нажатии на кнопку
.cssload-container {
text-align: center;
border-radius: 50%;
overflow: hidden;
display: flex;
}
.cssload-speeding-wheel {
width: 120px;
height: 120px;
margin: 0 auto;
border: 10px double;
border-radius: 50%;
position: relative;
}
.cssload-speeding-wheel:before,
.cssload-speeding-wheel:after{
content: "";
display: block;
width: 6px;
height: 150%;
background: white;
position: absolute;
z-index:1;
left: calc(50% - 3px);
top: -25%;
animation: cssload-spin 675ms infinite linear;
-o-animation: cssload-spin 675ms infinite linear;
-ms-animation: cssload-spin 675ms infinite linear;
-webkit-animation: cssload-spin 675ms infinite linear;
-moz-animation: cssload-spin 675ms infinite linear;
}
@keyframes cssload-spin {
100%{ transform: rotate(360deg); transform: rotate(360deg); }
}
@-o-keyframes cssload-spin {
100%{ -o-transform: rotate(360deg); transform: rotate(360deg); }
}
@-ms-keyframes cssload-spin {
100%{ -ms-transform: rotate(360deg); transform: rotate(360deg); }
}
@-webkit-keyframes cssload-spin {
100%{ -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}
@-moz-keyframes cssload-spin {
100%{ -moz-transform: rotate(360deg); transform: rotate(360deg); }
}
.cssload-speeding-wheel:after {
transform: rotate(90deg)
}
<div class="cssload-container">
<div class="cssload-speeding-wheel"></div>
</div>
<button>Click</button>
Ответы (2 шт):
Автор решения: Mihail
→ Ссылка
Без javascript не получится. Вот пример https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_animation
Автор решения: Михаил Камахин
→ Ссылка
Можно сделать анимацию, но не очень удобно.
Тут я использовал @property и изменение CSS-variables в @keyframes, это пока что работает только в Chromium браузерах. Но вы можете сделать такую же простую анимацию без CSS-variables
* {
box-sizing: border-box;
}
:root {
--transitionTimingFunction: ease;
--transitionDuration: 0.2s;
}
@property --percent {
syntax: '<integer>';
inherits: false;
initial-value: 0;
}
body {
font-family: sans-serif;
}
.input-group {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.checkbox {
display: none;
}
.checkbox-label {
border: 1px solid black;
padding: 5px 10px;
display: inline-block;
user-select: none;
cursor: pointer;
transition-timing-function: var(--transitionTimingFunction);
transition-duration: var(--transitionDuration);
transition-property: color, background-color, border-color;
}
.circle-svg {
display: block;
max-width: 50px;
--PI: 3.141592;
--r: 30;
--PiR2: calc( var(--PI) * var(--r) * 2);
}
.circle-svg circle {
r: var(--r);
}
.circle-svg__thumb {
fill: transparent;
stroke: transparent;
}
.circle-svg__track {
fill: transparent;
stroke: black;
stroke-width: 2px;
transform-origin: 50% 50%;
transform-box: fill-box;
transform: rotate(-90deg);
--percent: 0;
stroke-dasharray: calc(var(--PiR2) * var(--percent) / 100) var(--PiR2);
}
.checkbox:checked+.checkbox-label {
background-color: black;
border-color: black;
color: white;
}
.checkbox:checked+.checkbox-label+.circle-svg .circle-svg__track {
animation: circleAnimate 2s infinite alternate linear;
}
@keyframes circleAnimate {
0% {
--percent: 0;
}
100% {
--percent: 100;
}
}
<div class="input-group">
<input type="checkbox" class="checkbox" id="checkbox-1" hidden>
<label for="checkbox-1" class="checkbox-label">
<span>Кнопка</span>
</label>
<svg class="circle-svg" viewbox="0 0 64 64">
<circle class="circle-svg__thumb" cx="50%" cy="50%"> </circle>
<circle class="circle-svg__track" cx="50%" cy="50%"> </circle>
</svg>
</div>