【发布时间】:2018-10-15 21:38:53
【问题描述】:
现在在滚动页面时隐藏和显示页眉和页脚是很常见的,如果我们有离子信息,这就是解决方案 -
cli packages: (/usr/local/lib/node_modules)
@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0
global packages:
cordova (Cordova CLI) : 7.0.1
local packages:
@ionic/app-scripts : 3.1.9
Cordova Platforms : android 6.2.3
Ionic Framework : ionic-angular 3.9.2
System:
Node : v8.1.0
npm : 5.3.0
OS : Linux 4.4
在 provider 中创建一个名为 scroll 的文件,并在其中添加一个文件为 scroll-hide.ts -
import { Content } from 'ionic-angular';
import { Directive, ElementRef, Input, Renderer2, SimpleChanges } from '@angular/core';
@Directive({
selector: '[scrollHide]'
})
export class ScrollHideDirective {
@Input('scrollHide') config: ScrollHideConfig;
@Input('scrollContent') scrollContent: Content;
contentHeight: number;
scrollHeight: number;
lastScrollPosition: number;
lastValue: number = 0;
constructor(private element: ElementRef, private renderer: Renderer2) {
}
ngOnChanges(changes: SimpleChanges) {
if (this.scrollContent && this.config) {
this.scrollContent.ionScrollStart.subscribe((ev) => {
this.contentHeight = this.scrollContent.getScrollElement().offsetHeight;
this.scrollHeight = this.scrollContent.getScrollElement().scrollHeight;
if (this.config.maxValue === undefined) {
this.config.maxValue = this.element.nativeElement.offsetHeight;
}
this.lastScrollPosition = ev.scrollTop;
});
this.scrollContent.ionScroll.subscribe((ev) => this.adjustElementOnScroll(ev));
this.scrollContent.ionScrollEnd.subscribe((ev) => this.adjustElementOnScroll(ev));
}
}
private adjustElementOnScroll(ev) {
if (ev) {
ev.domWrite(() => {
let scrollTop: number = ev.scrollTop > 0 ? ev.scrollTop : 0;
let scrolldiff: number = scrollTop - this.lastScrollPosition;
this.lastScrollPosition = scrollTop;
let newValue = this.lastValue + scrolldiff;
newValue = Math.max(0, Math.min(newValue, this.config.maxValue));
this.renderer.setStyle(this.element.nativeElement, this.config.cssProperty, `-${newValue}px`);
this.lastValue = newValue;
});
}
}
}
export interface ScrollHideConfig {
cssProperty: string;
maxValue: number;
}
现在我们需要在 app.module.ts- 内的应用模块中声明它
并添加-
import { ScrollHideDirective } from '../providers/scroll/scroll-hide';
在页面顶部,现在是时候将@NgModule 部分包含为-
@NgModule({
declarations: [
...
ScrollHideDirective
],
imports: [
...
IonicModule.forRoot(MyApp, {})
],
providers: [
...
]
})
export class AppModule { }
移动到我们想要隐藏页脚和页眉的页面(home.ts),添加以下内容-
import { Component} from '@angular/core';
import { ScrollHideConfig } from '../../directives/scroll-hide';
@Component({
selector: 'page-explore',
templateUrl: 'explore.html'
})
export class ExplorePage {
footerScrollConfig: ScrollHideConfig = { cssProperty: 'margin-bottom', maxValue: undefined };
headerScrollConfig: ScrollHideConfig = { cssProperty: 'margin-top', maxValue: 44 };
...
}
现在我们必须在视图页面(home.html)中实现相同的功能
<ion-header [scrollHide]="headerScrollConfig" [scrollContent]="pageContent">
...
</ion-header>
<ion-content #pageContent fullscreen>
...
</ion-content>
<ion-footer [scrollHide]="footerScrollConfig" [scrollContent]="pageContent">
...
</ion-footer>
这一切都是为了完成我们的任务。 祝你有美好的一天!!!
【问题讨论】:
-
我投票决定将此问题作为题外话结束,因为这不是问题,而是作为问题发布的问题的解决方案。
-
问题可以在 Stack Overflow 上自行回答,但必须遵循问题/答案格式。请更改您的帖子,以便您在上面提出一个主题问题。然后,您可以在答案框中发布此答案。
-
我不知道怎么做,如果你能帮我一下就更好了。
-
您可以使用完整的问题文本作为答案,因此您可以将其复制并粘贴到答案框中,并添加当前答案的链接。棘手的部分是发布一个适合答案的好问题。如果您自己遇到问题,您可以询问如何解决它并展示失败的方法。
-
感谢您的帮助!
标签: javascript html ionic3 footer