我几乎按预期工作。然而,由于只有一个图像,当动画到左侧时,右侧会打开一个间隙。我需要的是,股票代码行总是用相同的图像填充,没有任何间隙。如果图像比容器窄,它应该向左和向右重复。
任何帮助或指导都非常感谢。
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条答案
按热度按时间2jcobegt1#
background-size:cover;您可以在.tickercontent中尝试此属性
k75qkfdt2#
个字符
hgc7kmma3#
JavaScript和CSS来创建一个从右向左动画的图像的滚动条效果。我注意到你只使用了一个图像元素,这会导致当它到达容器的左侧时出现一个间隙。
要解决此问题,您可以尝试使用两个图像元素而不是一个,并将它们放置在彼此旁边。然后,您可以使用CSS动画将它们都向左移动一个图像的宽度,然后将它们的位置重置为右侧。这样,您将始终在屏幕上看到一个图像,而另一个可以随时替换它。
下面是一个示例,说明如何执行此操作:
第一个月
字符串
CSS file :
个型
Js file :
个型
;