Google Chrome:同时“平滑”scrollIntoView() 与更多元素不起作用

2024-05-04

在谷歌浏览器中,element.scrollIntoView() with behavior: 'smooth'不能同时在多个容器上工作。一旦在一个容器上触发平滑滚动,第二个容器就会停止滚动。在Firefox中,这个问题不存在;两个容器可以同时滚动。

我的解决方法是使用behavior: 'instant',但我喜欢用behavior: 'smooth'为了更好的用户体验。

Example

Here https://plnkr.co/edit/MEUMMw8jyKt3KChxxZOl?p=preview是一个使用 Angular 的笨蛋

html

<p>
  In Google Chrome element.scrollIntoView() with behavior 'smooth' doesn't work, if scrolling more containers at the same time.
  Shwon in case 'All Smooth (200ms sequence)' the container stopps scrolling.
  <br>
  <br> In Firefox all works.
</p>

<div class="row mb-1">
  <div class="col">
    <button (click)="reset()" type="button" class="btn btn-secondary">Reset</button>
  </div>
</div>

<div class="row mb-1">
  <div class="col">
    <button (click)="scrollAllInstant()" type="button" class="btn btn-secondary">All Instant</button>
    <small class="text-success">Works</small>
  </div>
</div>

<div class="row mb-1">
  <div class="col">
    <button (click)="scrollAllSmooth()" type="button" class="btn btn-secondary">All Smooth (simultaneously)</button>
    <small class="text-danger">Only one container is scrolled</small>
  </div>
</div>

<div class="row mb-1">
  <div class="col">
    <button (click)="scrollAllSmoothSequenced()" type="button" class="btn btn-secondary">All Smooth (200ms sequence)</button>
    <small class="text-danger">Only last container is scrolled to 85 - Others will stop, if next container is triggered</small>
  </div>
</div>

<div class="row">
  <div *ngFor="let container of containers, let index = index" class="col">

    <button (click)="scrollSingelContainer(container)" type="button" class="btn btn-secondary mb-1">Single Container Smooth</button>
    <small class="text-success">Works</small>

    <div class="card bg-light mb-3" style="height: 500px;  max-width: 18rem;">
      <div class="card-header">Container {{ container }}</div>
      <div (scroll)="onScroll(container)" class="card-body" style="overflow-y: scroll;">
        <p *ngFor="let number of content" [attr.id]="container + '_' + number" class="card-text" [class.text-danger]="number == 85">{{ number }}</p>
      </div>

    </div>
  </div>
</div>

打字稿

export class App {
  name: string;

  containers = [0, 1, 2]
  content = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

  constructor() {
    this.name = `Angular v${VERSION.full}`
  }

  private scroll(container: number, row: number, behavior: string) {
    let element = document.getElementById(container + '_' + row);
    element.scrollIntoView({
      behavior: behavior,
      block: 'start',
      inline: 'nearest'
    });
  }

  reset() {
    this.containers.forEach(container => {
      this.scroll(container, 1, 'instant');
    });
  }

  scrollSingelContainer(container: number) {
    this.scroll(container, 85, 'smooth');
  }

  scrollAllInstant() {
    this.containers.forEach(container => {
      this.scroll(container, 85, 'instant');
    });
  }

  scrollAllSmooth() {
    this.containers.forEach(container => {
      this.scroll(container, 85, 'smooth');
    });
  }

  scrollAllSmoothSequenced() {
    this.containers.forEach(container => {
      setTimeout(() => {
        this.scroll(container, 85, 'smooth');
      }, 200 * container);
    });
  }

  onScroll(container: number) {
    console.log('Scroll event triggerd by container ' + container);
  }
}

这里也提出了类似的问题:scrollIntoView() 在 Chrome 中的多个元素上使用 smooth 函数 https://stackoverflow.com/questions/57214373/scrollintoview-using-smooth-function-on-multiple-elements-in-chrome,但答案并不令人满意,正如它所说,这不是一个错误。

但这似乎是一个错误,并且已经在 Chromium 错误列表中报告了:

  • https://bugs.chromium.org/p/chromium/issues/detail?id=1121151 https://bugs.chromium.org/p/chromium/issues/detail?id=1121151
  • https://bugs.chromium.org/p/chromium/issues/detail?id=1043933 https://bugs.chromium.org/p/chromium/issues/detail?id=1043933
  • https://bugs.chromium.org/p/chromium/issues/detail?id=833617 https://bugs.chromium.org/p/chromium/issues/detail?id=833617.

要同时平滑滚动多个元素,请使用scrollIntoView(至少对于某些元素)我们需要等待 Chromium 团队的修复。

An 替代方法正在使用scrollTo https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo它也适用于 Chrome 中的多个元素。请参阅本示例中的场景 5:https://jsfiddle.net/2bnspw8e/8/ https://jsfiddle.net/2bnspw8e/8/.

缺点是您需要获取要滚动到视图中的元素的下一个可滚动父级(请参阅https://stackoverflow.com/a/49186677 https://stackoverflow.com/a/49186677例如),计算将父级滚动到该元素所需的偏移量并调用parent.scrollTo({top: calculatedOffset, behavior: 'smooth'}).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Chrome:同时“平滑”scrollIntoView() 与更多元素不起作用 的相关文章

随机推荐