GithubHelp home page GithubHelp logo

浅说虚拟列表的实现原理 about blog HOT 25 OPEN

dwqs avatar dwqs commented on May 26, 2024 66
浅说虚拟列表的实现原理

from blog.

Comments (25)

tenadolanter avatar tenadolanter commented on May 26, 2024 8

测试了下,css里面元素的最大高度大概为33554400px,所以当列表数据非常多的时候,比如一千万,这种虚拟列表的实现方法无法滚动到最底部,这个算是一个bug吧

from blog.

Bravo123 avatar Bravo123 commented on May 26, 2024 5

发现两个问题
1.快速滚动或者直接拉到页面最下面,会直接白屏
2.this.cache会在来回滚动中越来越大

from blog.

blueYufisher avatar blueYufisher commented on May 26, 2024 1

请问一下当滑动过快的时候,页面会空白,这种如何解决

from blog.

jeffacode avatar jeffacode commented on May 26, 2024 1

看了下感觉应该不用这么麻烦,而且用的cache真的会越来越大
我的想法是这样的:

<div className="container" ref={containerRef} style={{ height: 400, width: 200 }}>
  <div className="box" style={{ height: itemHeight * allData.length }}>
    <div className="viewBox" style={{ paddingTop: startOffset }}>
      {data.map((d) => (
        <div key={d} className="item" style={{ height: itemHeight }}>
          {d}
        </div>
      ))}
    </div>
  </div>
</div>

image

from blog.

1zumii avatar 1zumii commented on May 26, 2024 1

@FAKER-A
excalidraw

from blog.

njleonzhang avatar njleonzhang commented on May 26, 2024

受教啦。有2点和你交流下啊:

  1. estimatedItemHeight 这个值是一个平均值的话,运气不好的话是否会出现问题呢?比如,前面几个都特别短,然后出现 item 没有占满屏幕。取一个元素的最小高度来计算 estimatedItemHeight,看起来最为保险,但是这样就导致要多渲染很多元素了。考虑到有 bufferSize 这个缓冲, estimatedItemHeight 的方案通常应该不会出什么问题。我这样理解应该对吧。😂

  2. 关于 bufferSize 这个值上一点提到了一个作用。另外一点,如果没有这个 bufferSize,那么用户往下滚动的时候,可能由于来不及渲染导致先看到一段空白,然后item才渲染出来吧。您文章提到的 bufferSize 是一个缓冲值,用来增加一定的缓存区域,让正常滑动速度的时候不会显得那么突兀。,应该是这个意思吧。

from blog.

dwqs avatar dwqs commented on May 26, 2024

@njleonzhang

  1. 你说的情况是可能存在的。estimatedItemHeight 取平均值是一个建议,对于你说的这种情况,可以不按照这个建议,譬如直接把 estimatedItemHeight 设置成 30 这样子(react-virtualized 貌似就是这个默认值) ,另外如果按照取平均值的建议,那可以将 buffer 设置大一些,我觉得也能有效避免你说的这种情况。

  2. 是的

from blog.

nanhupatar avatar nanhupatar commented on May 26, 2024

请问博客可以转载么,会注明出处和作者,没有看到文章共享协议,所以问一下

from blog.

dwqs avatar dwqs commented on May 26, 2024

@nanhupatar 可以

from blog.

ZhaZhengRefn avatar ZhaZhengRefn commented on May 26, 2024

看完之后实现了一份。不由得说,这个实现真的让人惊艳

from blog.

hkongm avatar hkongm commented on May 26, 2024

记得若干年前一届 QCon 天猫上去特地讲了这个主题
不止是列表 offset / height 这些
就连滚动贝塞尔函数都是经过精密计算出来的。。。因为要自己模拟滚动。。。

from blog.

haoxutong avatar haoxutong commented on May 26, 2024

楼主没有发现自己动态滚动实现方案有bug吗

from blog.

snakeUni avatar snakeUni commented on May 26, 2024

是否必须要设置可视区域的高度才可以啊。我尝试了 react-window,必须要设置高度

from blog.

Xekin97 avatar Xekin97 commented on May 26, 2024

虚拟列表还有个很极端的问题,就是浏览器的 Top 值和 height 都有一个极限的值,react-virtualize 的实现中,滚动都会修改列表中所有元素的 top 值,然后实现一个类似于放大镜的效果,在元素渲染时销毁后不会堆叠高度,不知道怎么才能优化得那么顺畅。

from blog.

chillerxx avatar chillerxx commented on May 26, 2024

我项目起来报错 require is not defined 我哭了

from blog.

nieshuangyan avatar nieshuangyan commented on May 26, 2024

from blog.

mjw-git avatar mjw-git commented on May 26, 2024

cache会越来越大,可以按照索引存一份,不要每次都push进去.

from blog.

TopAlien avatar TopAlien commented on May 26, 2024

cache会越来越大,可以按照索引存一份,不要每次都push进去.

LRUCache

from blog.

NoAlligator avatar NoAlligator commented on May 26, 2024

测试了下,css里面元素的最大高度大概为33554400px,所以当列表数据非常多的时候,比如一千万,这种虚拟列表的实现方法无法滚动到最底部,这个算是一个bug吧

这个不能算BUG吧?你原本那么多元素就支持不了这么长,这和加不加虚拟滚动没有关系吧?

from blog.

nieshuangyan avatar nieshuangyan commented on May 26, 2024

from blog.

Jetmet avatar Jetmet commented on May 26, 2024

from blog.

FAKER-A avatar FAKER-A commented on May 26, 2024

看了下感觉应该不用这么麻烦,而且用的cache真的会越来越大 我的想法是这样的:

<div className="container" ref={containerRef} style={{ height: 400, width: 200 }}>
  <div className="box" style={{ height: itemHeight * allData.length }}>
    <div className="viewBox" style={{ paddingTop: startOffset }}>
      {data.map((d) => (
        <div key={d} className="item" style={{ height: itemHeight }}>
          {d}
        </div>
      ))}
    </div>
  </div>
</div>

image

你好,想问下这图是用什么软件画的啊,很好看。

from blog.

dwqs avatar dwqs commented on May 26, 2024

from blog.

Jetmet avatar Jetmet commented on May 26, 2024

from blog.

nieshuangyan avatar nieshuangyan commented on May 26, 2024

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.