我只想证明我的ion-header
当页面滚动到特定点ionic2 - 滚动触发
HTML
<ion-header *ngIf="headered"><!--use ngif to trigger-->
<ion-navbar>
some content
</ion-navbar>
</ion-header>
所以这将是通过滚动触发:
TS
import { ..., Content } from 'ionic-angular';
...
@ViewChild(Content) content:Content;
headered = false;
...
ngAfterViewInit() {
this.content.addScrollListener(this.onPageScroll);
}
onPageScroll(event) {
console.log(event.target.scrollTop);
if(event.target.scrollTop > 640){
this.headered = true;
console.log(this.headered);///<-- this did trigger when scroll more than 640
}else{
this.headered = false;
}
}
我在控制台上得到了true
,但没有显示标题。我再次通过添加一个按钮调用这个函数来测试它:
toggleHeader(){
this.headered = (this.headered == false) ? true : false;
}
并且头部确实显示和隐藏了。
为什么scroll事件不能显示标题?我该如何解决这个问题?
Update01
我想:
scrollCount = 0;
...
onPageScroll(event) {
this.scrollCount = event.target.scrollTop;
console.log(this.scrollCount);///<-- this give me reading
}
///then use ngDoCheck to detect:
ngDoCheck(){
console.log(this.scrollCount);///<-- this always get 0.
}
正如你可以看到里面onPageScroll()
我得到读书出来的,我没有。我怀疑它与@ViewChild(Content) content:Content;
有关,ionic2
建议在their doc。
我想你的方法没有奏效。请看看我的更新。 – sooon
我已经更新了答案。请让我知道'resize()'方法是否有用。 – sebaferreras
错误:'无法读取未定义的属性'调整大小'。 – sooon