Profile Image Moving Up and Down
Now let's make the profile photo move infinitely up and down.
We define a @keyframes named bounce
and add bounce 1s infinite alternate
to the profile-image class as shown below.
/* Define the bounce animation */
@keyframes bounce {
from {
transform: translateY(0px); /* Starting position */
}
to {
transform: translateY(-10px); /* End position */
}
}
/* Apply the bounce animation */
.profile-image {
width: 150px;
height: 150px;
border-radius: 50%;
/* The bounce animation will repeat infinitely every 1 second, alternating directions */
animation: bounce 1s infinite alternate;
}
bounce 1s infinite alternate works as follows.
-
@keyframes bounce
: Defines an animation named bounce. This animation makes the element appear to bounce by applying specific transformations. -
from
andto
: Define the starting and ending stages of the bounce animation.from
specifies the start point of the animation, andto
specifies the end point. At each stage, thetransform
property is used to move the element vertically.translateY(0px)
indicates the original position of the element, whiletranslateY(-10px)
means the element is moved 10 pixels upward. -
In the
.profile-image
class, theanimation
property is used to make the bounce animation last for 1 second and repeat infinitely, with theinfinite
attribute. Thealternate
attribute makes the animation play forward and then backward in an alternating manner, creating a more natural up and down movement.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.