【发布时间】:2023-04-05 07:13:02
【问题描述】:
我正在尝试实现无限滚动重复背景图像的 CSS 动画。为了使滚动尽可能平滑,我希望动画在图像处于与开始时完全相同的位置时循环。为了让动画看起来不错,无论任何特定用户的视口大小如何,我都将使用 SVG 图形,并且我希望将 background-size 属性设置为 100% 或 cover 关键字。
我指定的参数没有按预期工作。但是,我选择的任何其他 background-size 值都可以。
为什么当background-size 是100% 时我的滚动动画不起作用?
有关工作示例,请参阅this code pen 或使用以下代码:
HTML
<div id="element">
<h1>Scroll endlessly I say!</h1>
</div>
CSS
@keyframes scroll {
0% { background-position: 0% center; }
100% { background-position: 100% center; }
}
#element{
padding: 10px;
color: white;
background: black;
text-shadow: 0 0 4px black;
animation: scroll 8s linear infinite;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/3/30/Vector-based_example.svg");
background-repeat: repeat;
/* Works:
========================= */
background-size: 50%;
/*background-size: 20%;*/
/*background-size: 200%;*/
/*background-size: 100px;*/
/*background-size: 10rem;*/
/* Does NOT work:
========================= */
/*background-size: 100%;*/
}
虽然我可以使用像 this one by CSS-Tricks 这样的其他解决方案,但我更喜欢我的解决方案,因为它更有效,因为我没有使用大型冗余图形。
【问题讨论】:
-
这真的很奇怪..但是你可以选择 50% 并且无论如何它都会响应
标签: css css-animations