【发布时间】:2021-10-28 14:12:55
【问题描述】:
我已经从我的 Angular 项目中创建了一个滚动到顶部组件,我试图让我的滚动图像仅在用户向下滚动后出现,但我的按钮仍然出现在初始加载中,所以任何想法或建议我怎么能做到这一点
所以我想做的是,
- 页面加载时,不显示滚动图标
- 当用户向下滚动页面时,显示滚动图标
HTML
<div class="to-top" (click)="scrollToTop()" [ngClass]="{ 'show-scrollTop': windowScrolled }">
<img src="../../../../assets/marketing-site/ecosystem/Scroll-to-Top.svg" alt="to-top-icon" />
</div>
CSS
.to-top {
bottom: 0;
cursor: pointer;
margin: 0 8px 12px 0;
position: fixed;
right: 0;
}
.show-scrollTop {
opacity: 1;
transition: all 0.2s ease-in-out;
}
TS
scrollToTop(): void {
// scroll to the top of the body
return this.document.body.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'start',
});
}
这是我创建的示例,请查看
https://stackblitz.com/edit/angular-ivy-2d5jrt?file=src/app/app.component.html
【问题讨论】:
-
我认为您在
.to-top课程中缺少opacity: 0。 -
@TotallyNewb 我刚刚添加但仍出现在初始加载中。如果用户仅向下滚动,我只希望按钮立即出现。 :(
标签: html css angular typescript scroll