滚动闪烁问题 - 使用scroll-snap型的CDK-Virtual-Scroll

发布于 2025-02-10 18:43:39 字数 747 浏览 1 评论 0 原文

我正在使用 cdk-virtual-scroll

它在不使用CSS属性 scroll-snap-type 的情况下工作正常。

当我使用滚动snap-type 时,它会闪烁几秒钟。我附上了代码样本 https://stackblitz.com/edit/Edit/Angular-gular-gchlgl?file=src%2fstyles.scss.scss,scs,src,src%2fapp; scroll-data-source-example.css,src%2FAPP%2fcdk-virtual-scroll-scroll-data-source-example.html

有帮助吗?谢谢。

I am using cdk-virtual-scroll.

It is working fine without using the css property scroll-snap-type .

When I use scroll-snap-type it is flickering few seconds. herewith I attached the code sample
https://stackblitz.com/edit/angular-gchlgl?file=src%2Fstyles.scss,src%2Fapp%2Fcdk-virtual-scroll-data-source-example.css,src%2Fapp%2Fcdk-virtual-scroll-data-source-example.html

Any help? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2025-02-17 18:43:39

如果您使用的是 cdk-virtual-scroll ,则应定义容器和物品的精确像素大小。因为 itextize 属性正在定义项目的大小。因此,在您的代码中,项目的大小相同,但响应大小(未使用像素固定),但您定义 itextize 为740px。我认为这将崩溃 cdk-virtual-scroll ,以提高网站的性能,同时计算渲染很多项目。

我已经更新您的代码并添加属性 itextize minbufferpx maxBufferpx

请参阅demo:

<cdk-virtual-scroll-viewport
  itemSize="500"
  class="example-viewport"
  minBufferPx="1000"
  maxBufferPx="5000"
>
  <div *cdkVirtualFor="let item of ds" class="example-item">
    <div class="info">{{item}}</div>
    <img
      src="https://images.pexels.com/photos/7913028/pexels-photo-7913028.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
      alt="Girl in a jacket"
    />
  </div>
</cdk-virtual-scroll-viewport>

stackblitz CSS属性到固定尺寸:

CSS
.example-viewport {
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  width: 100%;
  height: 500px; /* updated from 100vh */
}

.example-item {
  height: 500px; /* updated from 100vh */
  scroll-snap-align: start;
  margin: 0 !important;
  padding: 0 10px !important;
  position: relative;
}

.info {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  background-color: #0000008a;
  height: 25px;
  border-radius: 50px;
  padding: 0 10px;
  width: -webkit-fit-content;
  width: -moz-fit-content;
  width: fit-content;
  margin: 0 0 0 12px;
}

img {
  height: 500px; /* updated from 100vh */
  -o-object-fit: cover;
  object-fit: cover;
  -o-object-position: center center;
  object-position: center center;
}

If you are using cdk-virtual-scroll, you should define the exact pixel size of your container and item. Because itemSize attribute is defining the size of the item. So in your code, the size of the items are same but in responsive size (not fixed using pixel), but you define the itemSize is 740px. I thought that will be crash the cdk-virtual-scroll to improve performance of your website while doing the calculation of rendering a lot of items.

I've update your code and add attributes itemSize, minBufferPx, maxBufferPx:

SEE DEMO: StackBlitz

HTML
<cdk-virtual-scroll-viewport
  itemSize="500"
  class="example-viewport"
  minBufferPx="1000"
  maxBufferPx="5000"
>
  <div *cdkVirtualFor="let item of ds" class="example-item">
    <div class="info">{{item}}</div>
    <img
      src="https://images.pexels.com/photos/7913028/pexels-photo-7913028.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"
      alt="Girl in a jacket"
    />
  </div>
</cdk-virtual-scroll-viewport>

And update some responsive CSS properties to fixed size:

CSS
.example-viewport {
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  width: 100%;
  height: 500px; /* updated from 100vh */
}

.example-item {
  height: 500px; /* updated from 100vh */
  scroll-snap-align: start;
  margin: 0 !important;
  padding: 0 10px !important;
  position: relative;
}

.info {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  background-color: #0000008a;
  height: 25px;
  border-radius: 50px;
  padding: 0 10px;
  width: -webkit-fit-content;
  width: -moz-fit-content;
  width: fit-content;
  margin: 0 0 0 12px;
}

img {
  height: 500px; /* updated from 100vh */
  -o-object-fit: cover;
  object-fit: cover;
  -o-object-position: center center;
  object-position: center center;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文