Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

javascript - Ionic sliding between tabs template

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...