Css Анимации: попытка mvoe элемента div вверх и вниз и одновременное вращение элемента img внутри него не удалась - PullRequest
0 голосов
/ 14 апреля 2020

Описание проекта: я пытаюсь применить две анимации к вложенным изображениям внутри div, который на самом деле Div несет ответственность за перемещение изображения вверх и вниз, поскольку изображение захватывается внутри него И изображение (img), которое вложено в div, несет ответственность за последовательное вращение , в то время как div отскакивает изображение вверх и вниз .

Что я хочу :

1. изображение внутри элемента div должно вращаться на 360 градусов

2. Пока 1 происходит, Див должен продолжать подпрыгивать или двигаться вверх и вниз

.ground {
	position: absolute;
	width: 100%;
	height: 20px;
	background-color: gray;
	top: 800px;
}
.ball-container {
	position: relative;
	width 100px;
	height: 100px;
	left: 50%;
	animation-name: bounce;
	animation-duration: 1s;
	animation-fill-mode: forwards;
	animation-direction: forwards;
	animation-timing-function: linear;
	animation-iteration-count: infinite;
}
@keyframes bounce{
	0% {
		top: 0px;
	}
	50% {
		top: 700px;
		width: 130px;
		height: 70px;
	}
	100% {
		top: 0px;
	}
}
img {
	position: absolute;
	width: 100px;
	height: 100px;
	animation-name: rotation;
	animation-direction: forwards;
	animation-duration: 1s;
	animation-timing-function: linear;
	animation-fill-mode: forwards;
    animation-iteration-count: infinite;
}
@keyframes rotation {
	from {transform: rotate(0deg);}
	to {transform: rotate(360deg);}
}
<html>
	<div class="ball-container" id="ball-container"><img src="https://image.flaticon.com/icons/svg/53/53283.svg" alt="ball" class="ball" id="ball"/>
	</div>
	<div class="ground"></div>
</html>

Проблема: удивительный процесс отскока, но я не знаю, как заставить изображение вращаться, пока оно отскакивает .

Спасибо.

Codepen Link

ПОЧТА ИЗМЕНЕНА И НЕ ИМЕЕТ ПРОБЛЕМУ ПОСЛЕ ПРИМЕНЕНИЯ ОТВЕТА

1 Ответ

1 голос
/ 14 апреля 2020

animation-iteration-count должен быть бесконечным при вращении img, чтобы соответствовать количеству его отскоков, иначе анимация будет запускаться один раз и останавливаться, пока бокс все еще отскакивает. Также у вас есть опечатка, точка с запятой в to {transform: rotate(360deg;)} должна быть за пределами to {transform: rotate(360deg);}. Вот почему это не работает.

Кроме того, animation-direction:forwards недопустимо, правильное значение равно animation-direction:normal.

С этими исправлениями код:

.ground {
	position: absolute;
	width: 100%;
	height: 20px;
	background-color: gray;
	top: 800px;
}
.ball-container {
	position: relative;
	width 100px;
	height: 100px;
	left: 50%;
	animation-name: bounce;
	animation-duration: 1s;
	animation-fill-mode: forwards;
	animation-direction: normal;
	animation-timing-function: linear;
	animation-iteration-count: infinite;
}
@keyframes bounce{
	0% {
		top: 0px;
	}
	50% {
		top: 700px;
		width: 130px;
		height: 70px;
	}
	100% {
		top: 0px;
	}
}
img {
	position: absolute;
	width: 100px;
	height: 100px;
	animation-name: rotation;
	animation-direction: normal;
	animation-duration: 1s;
	animation-timing-function: linear;
	animation-fill-mode: both;
    animation-iteration-count: infinite;
}
@keyframes rotation {
	from {transform: rotate(0deg);}
	to {transform: rotate(360deg);}
}
<html>
	<div class="ball-container" id="ball-container"><img src="https://image.flaticon.com/icons/svg/53/53283.svg" alt="ball" class="ball" id="ball"/>
	</div>
	<div class="ground"></div>
</html>
...