У меня есть шаблон вкладок в ionic.Need, чтобы скрыть верхнюю панель навигации при прокрутке содержимого вкладки. Как и 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">
Установите событие on-scroll на ионное содержимое, а затем на ваш контроллер.
$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;
});
}
};
ЕСЛИ У ВАС ЕСТЬ ПРОСТОЙ БЛОК NAV И МОЖЕТ СКАЗАТЬ ЭТО ВСЕ ВМЕСТЕ ТОЛЬКО ИСПОЛЬЗОВАТЬ "$ 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.scrollmount - это всего лишь пиксельное значение, когда нужно скрыть навигационную панель, в этом случае пользователь прокручивает 180 пикселей сверху. Но после этого вам нужно добавить ng-if = "! HideNavigation" или ng-show = "! HideNavigation".
Вы также можете установить значение $ scope.scrollamount равным нулю /null, когда область действия уничтожена или 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 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 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';
}
});
}
}
На странице page.ts:
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
]
})
И в вашем page.html
<ion-content header-scroller>
Надеюсь, поможет!