简体   繁体   English

CSS滚动动画在所有文本滚动之前重新启动

[英]CSS scroll animation restarts before all text has scrolled

I'm trying to make a list of 100 paragraphs repeatedly scroll up, but the animation is restarting before the list finishes scrolling, at about 48 paragraphs. 我正在尝试重复向上滚动100个段落的列表,但动画在列表完成滚动之前重新启动,大约48个段落。 How can I make sure that all paragraphs scroll before the animation restarts? 如何在动画重新启动之前确保所有段落都滚动?

 div = document.getElementById("titlecontent"); for (c = 0; c < 100; c++) { str = c; p = document.createElement("p"); p.innerText = str; div.appendChild(p); } p = document.createElement("p"); p.innerText = "last p reached"; div.appendChild(p); 
 #titlecontent { position: absolute; top: 100%; -webkit-animation: scroll 10s linear 0s infinite; -moz-animation: scroll 10s linear 0s infinite; -ms-animation: scroll 10s linear 0s infinite; -o-animation: scroll 10s linear 0s infinite; animation: scroll 10s linear 0s infinite; } @-webkit-keyframes scroll { 0% { top: 100%; } 100% { top: -170%; } } @-moz-keyframes scroll { 0% { top: 100%; } 100% { top: -170%; } } @-ms-keyframes scroll { 0% { top: 100%; } 100% { top: -170%; } } @-o-keyframes scroll { 0% { top: 100%; } 100% { top: -170%; } } @keyframes scroll { 0% { top: 100%; } 100% { top: -170%; } } 
 <div id="titlecontent"></div> 

Your problem lies with top/bottom being related to the height of the screen, since the div is longer than those dimensions, it won't work. 你的问题在于顶部/底部与屏幕的高度有关,因为div比这些尺寸长,所以它不起作用。

I think I found a good solution, using only CSS. 我想我找到了一个很好的解决方案,只使用CSS。

Animating the top/bottom values is impossible, since CSS animations require their exact counterpart to animate, however, there is a property we can use to animate based on the entire height of the element 动画顶部/底部值是不可能的,因为CSS动画需要它们的精确对应动画,但是,我们可以使用一个属性来根据元素的整个高度进行动画处理

Introducing: CSS Transforms (translateX). 介绍:CSS Transforms(translateX)。

 div = document.getElementById("titlecontent"); for (c = 0; c < 100; c++) { str = c; p = document.createElement("p"); p.innerText = str; div.appendChild(p); } p = document.createElement("p"); p.innerText = "last p reached"; div.appendChild(p); 
 body { overflow: hidden; } body { overflow: hidden; } #titlecontent { animation: scroll 20s linear 0s infinite; } @-webkit-keyframes scroll { 0% { transform: translateY(10%); } 100% { transform: translateY(-100%); } } 
 <div id="titlecontent"></div> 

The magic happens in these lines: 魔术发生在以下几行:

  0% { transform: translateY(10%); }
  100% { transform: translateY(-100%); }

Instead of animating offset, we're animating the element's position on the X axis of the screen. 我们不是动画偏移,而是动画元素在屏幕X轴上的位置。 Making it -100% of it's actual height, and then animating it to 100% of it's actual height, effectively animating it offscreen before it repeats. 使其达到实际高度的-100%,然后将其设置为其实际高度的100%,在重复之前有效地将其设置为屏幕外的动画。

You just need to decide where the scrolling up should start, in this example 10% 你只需要确定滚动应该从哪里开始,在这个例子中10%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM