<div className="sound-wave-container py-8">
<img
src={!recording ? Wave : DottedLine}
className={!recording ? "sound-wave" : ""}
alt="Sound Wave"
/>
</div>
.sound-wave-container {
outline: 1px inset #999;
position: relative;
overflow: hidden;
width: 300px;
}
/* Define the animation */
@keyframes moveRightInfinity {
0% {
transform: translateX( 0%);
/* Start from the left boundary of the container */
}
100% {
transform: translateX( calc(100% - 100px));
/* Move to the right, but stop before it fully exits the view. Adjust the "100px" to the width of your image if it's different */
}
}
/* Apply the animation to the image */
.sound-wave {
animation: moveRightInfinity 10s linear infinite;
}
<div class="sound-wave-container py-8">
<img src="https://placekitten.com/200/300" class="sound-wave" alt="Sound Wave" />
</div>
currently it it going outside of div. How can i keep that inside frame and needs to continue in right side direction so it will look always full .
3
Answers
To achieve what you want you will need 2 separate images, as the first cannot appear at the left while still at the right. The images need to be positioned
absolute
for them to start from the same point, and ideally out of the container (-100px
here) so we don’t see them until the animation starts. This needs the container to have a height, because absolute positionned elements don’t take place and don’t extend the container’s size.You can use the same animation if it’s well calculated, the animation of the second image is delayed half the total duration. Note that I changed the % to px because % will apply to the size of the image, not the container.
To have the 300px element always ‘full’ you need a minimum of 3 of the 200px images you have chosen. [Think about what happens when an image is 50px across the element, there needs to be a bit of the image to the left and a bit to the right to fill the gaps].
To make the ‘arithmetic’ easy this snippet uses 4 copies of the image, and puts them into an inner div inside the 300px element.
It animates this inner div by just 50% of its width so that the animation looks continuous. The inner div is displayed flex so that the images are always on a line.