【发布时间】:2019-02-09 21:19:44
【问题描述】:
我已经实现了一种仅在向上滚动时显示按钮的方法:我的实现方式,感觉就像,它需要进行许多计算,因为逻辑会监听每个滚动事件。也许你们中的一些书呆子,有比我更好的方法。 :) 要求是:当页面最初加载或向上滚动时,按钮应显示在 UI 中。向下滚动时,该按钮应被隐藏。
我使用 Angulars @HostListener(..) 来监听滚动事件。
组件
public lastScrolledHeight: number = 0;
public showAddButton: boolean = true;
@HostListener('window:scroll', ['$event']) onScroll(event) {
const window = event.path[1];
const currentScrollHeight = window.scrollY;
console.log(currentScrollHeight);
if (currentScrollHeight > this.lastScrolledHeight) {
this.showAddButton = false;
console.log('should NOT show button');
} else {
this.showAddButton = true;
console.log('should show button');
}
this.lastScrolledHeight = currentScrollHeight;
}
HTML
<button class="btn btn-success btn-circle btn-xl"
[ngClass]="(showAddButton === true) ? 'scale-in' : 'scale-out'"
</button>
为了完成CSS:
.scale-out {
-webkit-animation: scale-out .2s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
animation: scale-out .2s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
}
.scale-in {
-webkit-animation: scale-in .2s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
animation: scale-in .2s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
}
期待任何输入。 :)
编辑:我创建了一个 Stackblitz 用于测试
【问题讨论】:
标签: javascript html css angular