在离子滚动时隐藏离子导航栏
Hide ion-nav-bar on scroll in ionic
我在ionic中有标签模板。滚动选项卡内容时需要隐藏顶部导航栏。就像whatsapp所做的那样。我的模板中需要哪些更改。
<!--This is Index.html-->
<body ng-app="starter">
<ion-nav-bar class="bar-positive"></ion-nav-bar>
<ion-nav-view></ion-nav-view>
</body>
<!--this is template.html-->
<ion-view>
<ion-content>
<ion-list>
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="x in names" type="item-text-wrap">
<img ng-src="{{x.displayProfile}}">
<h2>{{x.firstName}}</h2>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
这就是您在自定义情况下可以做到这一点的方法。
<ion-content on-scroll="scrollEvent()" delegate-handle="scrollHandle">
将滚动事件设置为离子内容,然后在控制器中设置。
$scope.scrollEvent = function() {
$scope.scrollamount = $ionicScrollDelegate.$getByHandle('scrollHandle').getScrollPosition().top;
if ($scope.scrollamount > 180) {
$scope.$apply(function() {
$scope.hideNavigation = true;
});
} else {
$scope.$apply(function() {
$scope.hideNavigation = false;
});
}
};
如果你有一个简单的导航栏,并且可以一起隐藏它,只需使用"$ionicNavBarDelegate.SHOWBAR(FALSE);"而不是$scope.HIDENavgiation和与该变量相关的所有内容。例:
$scope.scrollEvent = function() {
var scrollamount = $ionicScrollDelegate.$getByHandle('scrollHandle').getScrollPosition().top;
if (scrollamount > 0) { // Would hide nav-bar immediately when scrolled and show it only when all the way at top. You can fiddle with it to find the best solution for you
$ionicNavBarDelegate.showBar(false);
} else {
$ionicNavBarDelegate.showBar(true);
}
}
$scope.scrollamount只是何时隐藏导航栏的像素值,在这种情况下,当用户从顶部滚动180px时。但在此之后,您只需要添加 ng-if="!hideNavigation" 或 ng-show="!hideNavigation"。
您还可以在范围被破坏时将$scope.scrollamount设置为零/空或ionicview.leave等。
如果导航栏与模板不在同一模板和/或控制器中,则可以
$scope.scrollEvent = function() {
$scope.scrollamount = $ionicScrollDelegate.$getByHandle('scrollHandle').getScrollPosition().top;
if ($scope.scrollamount > 180) {
$scope.$apply(function() {
$rootScope.$broadcast('showHideNavigation', { hide: true });
});
} else {
$scope.$apply(function() {
$rootScope.$broadcast('showHideNavigation', { hide: false });
});
}
};
并在您的其他控制器中捕获它
$scope.$on('showHideNavigation', function(event, args) {
$scope.hideNavigation = args.hide;
});
希望这对您有所帮助。
只是想用我制作的东西为 Ionic 2 用户做出贡献。
您只需要将其添加到您的项目中。之后,您可以自定义 HeaderScroller 组件:
header-scroller.ts:
import { Directive, ElementRef } from '@angular/core';
/**
* Show/Hide header based on page's scroll position.
*/
@Directive({
selector: '[header-scroller]'
})
export class HeaderScroller {
/**
* @var number Distance from page top to trigger the hide/show functionality.
*/
protected _topTriggeringDistance: number = 100;
/**
* @var string Distance to keep between the top and the content when the header is hidden.
*/
protected _topContentDistance: string = '10px';
/**
* @var number Animation transition, in seconds, for showing/hiding the header.
*/
protected _animationTransition: number = 0.6;
/**
* @var HTMLElement Content element (<ion-content>).
*/
protected _el: any;
/**
* Initializes.
*
* @param el ElementRef Directive's selector.
* @return void
*/
constructor(el: ElementRef) {
this._el = el.nativeElement;
}
/**
* Binds the scroller.
*
* @return void
*/
ngOnInit() {
// Set animation transition
this._el.previousElementSibling.style.transition = `top ${this._animationTransition}s ease-in-out`;
this._bindScroller(this._el.children[0]);
}
/**
* Listen to scroll events in <scroll-content> component.
*
* @param el HTMLElement Scroller element (<scroll-content>).
* @return void
*/
protected _bindScroller(el): void {
el.addEventListener('scroll', event => {
let scroller = event.target,
header = event.target.parentElement.previousElementSibling;
if (event.target.scrollTop > this._topTriggeringDistance && header.style.top != '0') {
scroller.style.marginTop = this._topContentDistance;
header.style.top = `-${header.offsetHeight}px`;
} else if (event.target.scrollTop <= this._topTriggeringDistance && header.style.top == `-${header.offsetHeight}px`) {
header.style.top = '0';
scroller.style.marginTop = `${header.offsetHeight}px`;
}
});
}
}
在您的页面中:
import { HeaderScroller } from 'path/to/header-scroller';
根据您的 Ionic 版本(测试版或稳定版),您必须添加:
// Ionic 2 beta (page.ts)
@Component({
directives: [ HeaderScroller ]
})
// Ionic 2 new (app.module.ts) -untested, but it should work-
@NgModule({
declarations: [
HeaderScroller
],
entryComponents: [
HeaderScroller
]
})
在您的页面中.html
<ion-content header-scroller>
希望对您有所帮助!
相关文章:
- 显示放大镜弹出窗口时隐藏导航内容
- 导航栏隐藏在图片后面
- 在离子滚动时隐藏离子导航栏
- jquery导航显示和隐藏多个页面
- 导航关闭时隐藏文本导航
- 隐藏在导航栏中的下拉菜单
- jQuery/JS正在获取导航菜单,以便在单击正文时隐藏
- jQuery 循环 如果只有一张幻灯片,则隐藏上一个/下一个导航
- 使用 JavaScript 显示和隐藏导航栏进行滚动和悬停功能
- 响应式导航在页面加载时打开,然后隐藏.应该在页面加载时隐藏
- 在垂直滚动时隐藏/显示导航
- Angularjs:隐藏包含动态参数的网址的导航栏
- 如果导航选项卡内容为空,则无法隐藏导航选项卡
- jquery切换隐藏/显示导航元素
- 粘性导航元素 - 隐藏然后显示
- 谷歌地图API - 通过类别导航隐藏所有信息窗口
- 如何让我的移动菜单按钮显示和隐藏导航栏,以及在负空间中单击时隐藏
- 使用 CSS 在导航栏上创建“不可见”滚动条(隐藏不起作用)
- CSS部分固定侧导航隐藏时主要内容较短
- 移动导航隐藏/显示一个按钮