Reputation: 561
I downloaded Ionic tabs template. I played around with it for a while and am wondering how to add a sliding function that can navigate between tabs. In this case, I want to slide with animations and a smooth transition, not a sudden tab switch.
I saw on Ionic Docs. It has this slide box feature.
<ion-slide-box on-slide-changed="slideHasChanged($index)">
<ion-slide>
<div class="box blue"><h1>BLUE</h1></div>
</ion-slide>
<ion-slide>
<div class="box yellow"><h1>YELLOW</h1></div>
</ion-slide>
<ion-slide>
<div class="box pink"><h1>PINK</h1></div>
</ion-slide>
</ion-slide-box>
But then I am using tabs template from Ionic
<ion-tabs>
<ion-tab>
<ion-nav-view>
</ion-nav-view>
</ion-tab>
How would I be able to incorporate these to together?
Thank you!
Upvotes: 1
Views: 2245
Reputation: 110
can be done easily on IONIC tabs structure.
firstly, you need to add controller to your tab in app.js:
.state('tab', {
url: '/tab',
abstract: true,
controller: 'TabsCtrl',
templateUrl: 'views/base/tabs.html'
})
then, you need to modify your tabs.html with on-swipe-left, on-swipe-right attr and classes: 'active-tab-{{ activeTab }}"' to your ion-tabs and 'nav-tab-0' for you first ion-nav-view . Last number have to increase in next ion-nav-views.
So your tabs.html should look like this:
<ion-tabs class="tabs-icon-only tabs-color-active-positive active-tab-{{ activeTab }}" on-swipe-left="onSwipeLeft()" on-swipe-right="onSwipeRight()">
<ion-tab title="Notifications" icon-off="ion-flag" icon-on="ion-flag" ui-sref="tab.notifications">
<ion-nav-view name="tab-notifications" class="nav-tab-0"></ion-nav-view>
</ion-tab>
<ion-tab title="Profile" icon-off="ion-person" icon-on="ion-person" ui-sref="tab.profile">
<ion-nav-view name="tab-profile" class="nav-tab-1"></ion-nav-view>
</ion-tab>
<ion-tab title="Home-Map" icon-off="ion-map" icon-on="ion-map" ui-sref="tab.home_map">
<ion-nav-view name="tab-home-map" class="nav-tab-2"></ion-nav-view>
</ion-tab>
<ion-tab title="Home-List" icon-off="ion-ios-list" icon-on="ion-ios-list" ui-sref="tab.home_list">
<ion-nav-view name="tab-home-list" class="nav-tab-3"></ion-nav-view>
</ion-tab>
<ion-tab title="Create" icon-off="ion-plus-round" icon-on="ion-plus-round" ui-sref="tab.create_event">
<ion-nav-view name="tab-create-event" class="nav-tab-4"></ion-nav-view>
</ion-tab>
</ion-tabs>
Next step is to fill you controller. Here you need to have to table of states names in order in your nav. Heres my code:
$scope.tabList = [
'tab.notifications',
'tab.profile',
'tab.home_map',
'tab.home_list',
'tab.create_event'
];
$scope.onSwipeLeft = function() {
for(var i=0; i < $scope.tabList.length; i++) {
if ($scope.tabList[i] == $state.current.name && i != $scope.tabList.length){
$state.go($scope.tabList[i+1]);
break;
}
}
};
$scope.onSwipeRight = function() {
for(var i=0; i < $scope.tabList.length; i++) {
if ($scope.tabList[i] == $state.current.name && i != 0){
$state.go($scope.tabList[i-1]);
break;
}
}
};
$scope.$on('$ionicView.enter', function() {
for(var i=0; i < $scope.tabList.length; i++) {
if ($scope.tabList[i] == $state.current.name){
$scope.activeTab = i;
break;
}
}
});
Last this is the animation and hidding ion-nav-views, all that you can do in your scss files like this :
.tab-content {
display: block;
opacity: 0;
@include transition-property('left, right');
@include transition-duration(.3s);
}
$tabs-count: 5;
@for $i from 0 through $tabs-count {
@for $j from 0 through $tabs-count {
.active-tab-#{$i} {
@if $i == $j {
.nav-tab-#{$j} {
opacity: 1;
left: 0 !important;
right: 0 !important;
}
}
@if $i > $j {
.nav-tab-#{$j} {
left: -100%;
right: 100%;
}
}
@if $i < $j {
.nav-tab-#{$j} {
left: 100%;
right: -100%;
}
}
}
}
}
So all that togather make it works like native tab app. I hope I helped, This solutions works great for me.
Upvotes: 4
Reputation: 2940
Use on-swipe-right="goTo('tabs.tab3')"
and on-swipe-left="goTO(tabs.tab1)"
and the javascript is
.run(function($rootScope, $state){
$rootScope.goTo = function(stateName){
/*$state.go(stateName)*/
alert(stateName);
$state.go(stateName);
}
})
Upvotes: 0