【发布时间】:2021-02-20 18:31:02
【问题描述】:
我在一个 Angular 项目中创建了一个示例 scrollIntoView。它在网络浏览器上运行良好。但是,当您在移动设备(Chrome 或 Safari)上查看它时,该行为是不稳定的。它会直接跳到没有“平滑”行为的位置。
app.component.html
<button (click)="scroll('one')">One</button>
<button (click)="scroll('two')">Two</button>
<button (click)="scroll('three')">Three</button>
<button (click)="scroll('four')">Four</button>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
app.component.ts
export class AppComponent {
name = 'Mobile scrollIntoView';
scroll(id: string) {
console.log(`scrolling to ${id}`);
const el = document.getElementById(id);
el.scrollIntoView({behavior: 'smooth', block: 'start', inline: 'nearest'});
}
}
您可以在 stackblitz 上查看 - 用手机打开:https://angular-nbpxk7.stackblitz.io
编辑模式:https://stackblitz.com/edit/angular-nbpxk7
我是否遗漏了什么,或者如何在移动设备上存档“流畅”行为?谢谢,
【问题讨论】:
-
你没有错过任何东西。只是因为 scrollIntoView 不支持 iOS Safari 上的
smooth行为。 caniuse.com/#feat=scrollintoview -
谢谢托安。有没有其他选择?
标签: javascript angular scroll