【发布时间】:2022-01-21 20:54:40
【问题描述】:
我尝试使用 Angular 和 OnScroll 事件创建一种视差,但是在滚动时出现的文本闪烁。你知道如何让 onscroll 的效果渲染更流畅吗?也许只有 CSS ?
创建的脚本 https://stackblitz.com/edit/angular-b5cmf7?file=src%2Fapp%2Fapp.component.html
使效果成为可能的主要功能就是这个
@HostListener('window:scroll', ['$event'])
isScrolledIntoView(){
const offsetHeight = this.testDiv.nativeElement.children[0].offsetHeight;
const rect = this.testDiv.nativeElement.getBoundingClientRect();
if( rect.bottom <= window.innerHeight){ // when the div is visible in viewport change the height (not more than the height of the content)
let height = (window.innerHeight - rect.bottom);
if(height<offsetHeight){
this.height = height;
}else{
this.height = offsetHeight
}
}
}
【问题讨论】:
-
视差滚动不一定需要javascript。 See this link