I have it almost working as expected. However, since there is only one image, when animates to the left side, a gap opens up on the right side. What I need is that the ticker line would always be filled in with the same image, without any gaps.
If image is narrower than the container, it should repeat to left and right.
Any help or guidance is much appreciated.
const tickerImage = document.getElementById('ticker-image');
const tickerLink = document.getElementById('ticker-link');
let isRotating = true;
tickerLink.addEventListener('click', function(e) {
if (isRotating) {
e.preventDefault(); // Prevent the link from navigating if it's rotating
}
});
tickerLink.addEventListener('mouseover', function() {
isRotating = false;
tickerImage.style.animationPlayState = 'paused';
});
tickerLink.addEventListener('mouseout', function() {
isRotating = true;
tickerImage.style.animationPlayState = 'running';
});
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.ticker {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background-color: #333;
color: #fff;
padding: 10px 0;
text-align: center;
cursor: pointer;
user-select: none;
overflow: hidden;
}
.ticker-content {
text-decoration: none;
display: block;
width: 100%;
/* Set the width to 100% */
height: 100%;
/* Set the height to 100% */
position: relative;
overflow: hidden;
}
.image-container {
display: flex;
width: 100%;
white-space: nowrap;
animation: slideImage 10s linear infinite;
}
.image-container img {
display: inline-block;
width: 100%
}
@keyframes slideImage {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(-100%);
}
}
<div class="ticker">
<a href="https://example.com" target="_blank" class="ticker-content" id="ticker-link">
<div class="image-container">
<img src="https://images.all-free-download.com/images/graphiclarge/beach_cloud_dawn_horizon_horizontal_landscape_ocean_601821.jpg" alt="Rotating Image" id="ticker-image">
</div>
</a>
</div>
3
Answers
background-size: cover; you can try this property in the .tickercontent
JavaScript and CSS to create a ticker effect with an image that animates from right to left. I noticed that you are using only one image element, which causes a gap when it reaches the left side of the container.
To fix this issue, you could try using two image elements instead of one, and position them next to each other. Then, you can use CSS animations to move them both to the left by the width of one image, and then reset their positions to the right. This way, you will always have one image visible on the screen, and the other one ready to replace it.
Here is an example of how you could do this:
HTML file :
CSS file :
Js file :
;