英文:
Marquee animation is resetting
问题
动画运行得很完美,但在一段时间后,它会返回到初始状态 "0%",然而我想要的是它无限循环继续播放。
运行这段代码片段并观察一段时间,可以看到效果完美,即无限向左移动项目,然后返回向右,但是在某一时刻会发生 "闪烁" 并重新启动动画和项目。
div.app-marquee-container {
position: relative;
cursor: default;
display: flex;
justify-content: space-between;
border-bottom: 1px solid var(--outline);
background-color: var(--neutral-base);
-webkit-user-select: none; /* Safari */
-ms-user-select: none; /* IE 10 and IE 11 */
user-select: none; /* Standard syntax */
}
div.app-marquee-container:hover {
cursor: pointer;
}
div.app-marquee-container:hover div.marquee-config-action-container {
display: block;
}
div.marquee-container {
position: relative;
width: 100%;
}
div.marquee-subcontainer {
position: absolute;
display: flex;
align-items: center;
gap: 16px;
animation: marquee 50s linear infinite forwards;
}
div.marquee-config-action-container {
right: 0;
position: absolute;
display: none;
width: 40px;
box-shadow: 0px 2px 12px 0px #3944561F;
background-color: var(--neutral-base);
}
ul {
height: 40px;
width: 80%;
list-style: none;
display: flex;
align-items: center;
gap: 16px;
}
ul li {
width: 200px;
display: flex;
justify-content: center;
align-items: center;
gap: 4px;
}
ul li span {
font-size: 12px;
font-weight: 400;
}
@keyframes marquee {
from { -webkit-transform: translateX(0vw) }
to { -webkit-transform: translateX(-100vw) }
}
<div class="app-marquee-container">
<div class="marquee-container">
<div class="marquee-subcontainer">
<!-- 此处省略了一些项目 -->
</div>
<div class="marquee-subitem-container">
<!-- 此处省略了一些项目 -->
</div>
</div>
<div class="marquee-config-action-container">
<button>FAKE</button>
</div>
</div>
英文:
The animation works perfectly, however, after a certain x period of time it returns to the initial state of "0%", however what I want is for it to be infinite and continue.
Run the code snippet and watch it for a certain time, it can be seen that the effect works perfectly which is taking the items to the left infinitely and coming back to the right, however, at a certain moment, a "flicker" will happen and it will restart the animation and also the items.
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-css -->
div.app-marquee-container {
position: relative;
cursor: default;
display: flex;
justify-content: space-between;
border-bottom: 1px solid var(--outline);
background-color: var(--neutral-base);
-webkit-user-select: none; /* Safari */
-ms-user-select: none; /* IE 10 and IE 11 */
user-select: none; /* Standard syntax */
}
div.app-marquee-container:hover {
cursor: pointer;
}
div.app-marquee-container:hover div.marquee-config-action-container {
display: block;
}
div.marquee-container {
position: relative;
width: 100%;
}
div.marquee-subcontainer {
position: absolute;
display: flex;
align-items: center;
gap: 16px;
animation: marquee 50s linear infinite forwards;
}
div.marquee-config-action-container {
right: 0;
position: absolute;
display: none;
width: 40px;
box-shadow: 0px 2px 12px 0px #3944561F;
background-color: var(--neutral-base);
}
ul {
height: 40px;
width: 80%;
list-style: none;
display: flex;
align-items: center;
gap: 16px;
}
ul li {
width: 200px;
display: flex;
justify-content: center;
align-items: center;
gap: 4px;
}
ul li span {
font-size: 12px;
font-weight: 400;
}
@keyframes marquee {
from { -webkit-transform: translateX(0vw) }
to { -webkit-transform: translateX(-100vw) }
}
<!-- language: lang-html -->
<div class="app-marquee-container">
<div class="marquee-container">
<div class="marquee-subcontainer">
<div class="marquee-subitem-container">
<ul>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
</ul>
</div>
<div class="marquee-subitem-container">
<ul>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
<li>
<span>MSFT34</span>
<span>1200.24</span>
<span>100%</span>
</li>
</ul>
</div>
</div>
</div>
<div class="marquee-config-action-container">
<button>FAKE</button>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
以下是翻译好的部分:
"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:
The marquee effect you're trying to achieve works by translating the items to the left (using the translateX(-100vw)), and since it's set to infinite, it restarts from the beginning once it reaches the end. The "flicker" or reset you see is the animation starting over from the beginning.
要实现你想要的跑马灯效果,它通过将项目向左平移(使用 translateX(-100vw))来实现,因为它被设置为无限循环,所以当它达到末尾时会重新开始。你看到的“闪烁”或重置是动画从头开始的表现。
To achieve a seamless infinite scroll effect without this "flicker", the content itself must be duplicated so that by the time the original content has finished scrolling out of view, the duplicate content starts to appear.
为了实现无缝的无限滚动效果,而没有这种“闪烁”,内容本身必须被复制,这样当原始内容完成滚动并移出视野时,复制的内容就开始出现。
Here's a solution:
以下是一个解决方案:
Duplicate the content, which it looks like you have already.
复制内容,看起来你已经做了。
Adjust the keyframes such that the content scrolls by 50% (halfway) and not the full 100%. Because, by the time you reach halfway, the duplicated content will begin and the cycle continues.
调整关键帧,使内容滚动50%(一半),而不是100%。因为当你达到一半的时候,复制的内容将开始出现,循环继续。
Update the keyframe like this:
像这样更新关键帧:
@keyframes marquee {
0% { transform: translateX(0%); }
100% { transform: translateX(-50%); }
}
@keyframes marquee {
0% { transform: translateX(0%); }
100% { transform: translateX(-50%); }
}
The .marquee-subcontainer will hold twice the original content. When the animation completes one cycle (50% scroll), the second copy will be at the starting position, hence creating an illusion of an infinite scroll. Since you already have duplicated the content in your HTML, the only required change is in the keyframes as mentioned above.
.marquee-subcontainer 将包含两倍于原始内容的内容。当动画完成一个循环(50%滚动)时,第二份复制将位于起始位置,从而创建了无限滚动的错觉。由于你已经在HTML中复制了内容,唯一需要更改的是上述提到的关键帧。
I should also mention this method works best when the content is evenly divisible into the viewport, so you might need to adjust the content length or viewport width to achieve a perfectly seamless transition. Keep that in mind.
我还应该提到,当内容能够均匀分割到视口时,这种方法效果最佳,因此你可能需要调整内容长度或视口宽度以实现完全无缝的过渡。请记住这一点。
If you plan to change the data to something dynamic rather than typed out explicitly like that, this will be much easier.
如果你计划将数据更改为动态内容而不是像这样明确输入,那么这将更容易实现。"
英文:
The marquee effect you're trying to achieve works by translating the items to the left (using the translateX(-100vw)), and since it's set to infinite, it restarts from the beginning once it reaches the end. The "flicker" or reset you see is the animation starting over from the beginning.
To achieve a seamless infinite scroll effect without this "flicker", the content itself must be duplicated so that by the time the original content has finished scrolling out of view, the duplicate content starts to appear.
Here's a solution:
Duplicate the content, which it looks like you have already.
Adjust the keyframes such that the content scrolls by 50% (halfway) and not the full 100%. Because, by the time you reach halfway, the duplicated content will begin and the cycle continues.
Update the keyframe like this:
@keyframes marquee {
0% { transform: translateX(0%); }
100% { transform: translateX(-50%); }
}
The .marquee-subcontainer will hold twice the original content. When the animation completes one cycle (50% scroll), the second copy will be at the starting position, hence creating an illusion of an infinite scroll. Since you already have duplicated the content in your HTML, the only required change is in the keyframes as mentioned above.
I should also mention this method works best when the content is evenly divisible into the viewport, so you might need to adjust the content length or viewport width to achieve a perfectly seamless transition. Keep that in mind.
If you plan to change the data to something dynamic rather than typed out explicitly like that, this will be much easier.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论