虚拟列表中的(仅新的)元素的动画输入
我正在使用react-window
filestsizedlist
和react-virtualized-auto-sizer
autosizer autoSizer 组件构建list/list/list/list/表UI元素可能包含数千个项目,这些项目也通过WebSocket连接接收新项目并将其预先列入列表。我现在需要对此列表中的新元素的输入进行动画。
这是一个具有最小(并且不够有效的)示例的代码和框链接: noreflow noreferrer“> codesandbox 。
请注意,。还要注意
。
我知道发生这种情况的原因是因为列表如何使用孩子行的绝对定位来实现虚拟化。每次将新项目插入数据或滚动列表时,react-window
都需要重新计算每一行的样式
prop,从而重新创建每个DOM元素因此,重新启动.ROW
动画。
我想仅动画新的
。
- >到
.ROW
类。由于上述每个元素都在新项目插件上重新创建,因此这与上述相同的原因不起作用。 - 仅动画第一行(示例使用代码沙箱中的红色背景)。虽然“有效”,但它并不十分合适。在我的生产站点中,不保证一次更新一次 - 可能同时插入多行。将所有新行添加到DOM时,都应将它们动画。可以通过在挂钩中立即插入两个UUID来在代码沙箱中复制这一点。
- 不使用列表虚拟化。这当然可以正常工作,但不合适。在我的生产站点中,此列表可能包含数千个项目。
- 阅读以前的问题。上一个问题的信息很少,没有最小的示例,也没有有用的答案或评论。此外,它已经超过5年了。
如何实现我在这里寻找的结果?
编辑:
我尝试了进一步的尝试,在2)上进行了延伸:
- 保存每个渲染项目的“旧”列表的副本。收到更新后,从新列表的长度中减去旧列表的长度(调用此号码
n
)。动画新列表的顶部n
项目。此“有效”,但是生产系统具有一些复杂的解决方案,使该解决方案不足 - 每个列表项目都有ISO时间戳的日期,并且根据时间戳new -&gt对列表进行排序;老的。通过WebSocket收到的更新还具有时间戳,,但不能保证新项目总是比当前列表的最新产品更新 - 在某些情况下,将新项目插入2、3或更远的位置列表。在这种情况下,基于长度更改的顶部n
项目将不准确。
I am using react-window
FixedSizedList
and react-virtualized-auto-sizer
Autosizer
components to build a list/table UI element that may contain thousands of items that also receives new items via a websocket connection and prepends them to the list. I now have a requirement to animate the entry of new elements in this list.
Here is a codesandbox link with a minimal (and not quite working) example: codesandbox.
Note how the .row
animation is triggered for every child of FixedSizedList
each time the data list receives a new item. Also note how the .row
animation is again triggered for every child of FixedSizedList
when the list is scrolled.
I understand that the reason this is happening is because of how list virtualization works using absolute positioning of the children rows. Every time a new item is inserted into data, or the list is scrolled, react-window
needs to re-compute the style
prop for each row, which recreates every DOM element and hence re-triggers the .row
animation.
I would like to animate only the new .row
DOM elements when they appear.
Things I have tried:
- Add
animation-iteration-count: 1;
to the.row
class. This doesn't work for the same reason mentioned above as every element is re-created on a new item insert. - Animate only the first row (example using red background in the code sandbox). While this "works", it's not quite suitable. In my production site, updates are not guaranteed to come through one at a time - multiple rows might be inserted at the same time. All new rows should be animated when they are added to the DOM. This can be replicated in the code sandbox by inserting two UUID's at once in the hook.
- Not use list virtualization. This of course works fine, but is not suitable. In my production site this list could contain thousands of items.
- Reading this previous question. The previous question is sparse of information, does not have a minimal example, and has no useful answers or comments. Additionally, it is over 5 years old.
How is it possible to achieve the result I'm looking for here?
EDIT:
A further attempt I have tried, extending on 2) above:
- Save a copy of the "old" list of items each render. When an update is received, subtract the length of the old list from the length of the new list (call this number
n
). Animate the topn
items of the new list. This "works", but the production system has some intricacies making this solution insufficient - each list item is dated with an ISO timestamp, and the list is sorted according to timestamp new -> old. Updates received via websocket also have a timestamp, but there is no guarantee the new item will always be newer than the current top of the list - in some cases, new items are inserted into position 2, 3 or further down in the list. In this case, animating the topn
items based on the length change would not be accurate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道对您来说有多昂贵,但是在每个状态更改上,一种做到这一点的方法是将特定的
className
添加到新的插入行和className
已经插入的行。因此,这样您就可以使用与新插入线相关的
className
处理。类似的东西:
这是代码示例向您检查。
Don´t know how costly it can be for you, but one way to do that is on every state change add a specific
className
to the new insert rows and aclassName
to the already inserted rows.So that way you can handle using the
className
related to the new inserted lines.Something like this:
Here's a code sample to you check.
如您所说,如果列表项目具有唯一的ID,则您只需要在每次更新时比较ID的更新,并对与这些添加或减少ID相对应的列表项进行动画。
As you said, if the list items have a unique id, then you only need to compare the update of the ids each time you update, and perform animation on the list items corresponding to these added or decreased ids.