【发布时间】:2021-02-14 12:49:27
【问题描述】:
在我的网络应用程序中,我尝试在滚动事件上设置 div 的高度以产生 UI 效果和视差, 当我滚动到顶部时,Div 会变大,当我滚动到底部页面时,Div 会变小以获得视差效果。
我使用 @HostListener('window:scroll', ['$event']) 触发滚动,它的工作,但结果是冻结帧如此生涩,随机延迟.. .
有人知道为什么会有这么随机的错误吗?或者角度库可以通过性能优化来做到这一点?
component.html:
<div id="header" class="header" >
<div class="coverBackgroundGradiantUp"> </div>
<div class="coverBackgroundGradiantDown">
</div>
</div>
<div id="panel" class="panel" (scroll)="scrollHandler($event)">
{...bodypage...}
</div>
component.ts:
export class ProfilComponent implements OnInit {
constructor() {
}
ngOnInit(): void {
}
@HostListener('window:scroll', ['$event'])
scrollHandler(event) {
console.log(window.pageYOffset)
const offset = window.pageYOffset
if (offset < -1) {
document.getElementById("headerHeight").style.width = String((100 - offset/6)+ "%");
document.getElementById("headerHeight").style.height = String( 151 - offset + "px");
} else if (offset < 151) {
document.getElementById("headerHeight").style.height = String( 151 - offset + "px");
} else if (offset > 151) {
document.getElementById("headerHeight").style.height = String(0 + "px");
}
}
组件.scss:
.header{
position: fixed;
display: flex;
flex-direction: column-reverse;
background-image: url("../../../../../assets/exempleCover.png");
background-size: cover;
background-repeat: no-repeat;
width: 100%;
height: 151px;
top: 59px;
background-color: red;
}
......
.panel{
position: relative;
top: 0;
width: auto;
margin-top: 146px;
z-index: 2;
}
【问题讨论】:
标签: javascript angular animation scroll offset