可能是由于Angular 15中引入了Zone.js导致的问题。需要在polyfills.ts
中添加web-animations-js
来解决问题。具体步骤如下:
web-animations-js
:npm install --save web-animations-js
polyfills.ts
中引入:import 'web-animations-js'; // 在顶部添加
position: sticky
的元素上添加top
属性:.sticky-element {
position: sticky;
top: 0;
}
完整代码示例:
// polyfills.ts
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'web-animations-js'; // 在顶部添加
// app.component.html
Sticky element
Other element
// app.component.css
.sticky-container {
height: 800px;
}
.sticky-element {
position: sticky;
top: 0;
background-color: yellow;
padding: 10px;
}
.other-element {
height: 400px;
background-color: blue;
color: white;
padding: 10px;
}