Skip to content

Instantly share code, notes, and snippets.

@guannie
Last active November 27, 2018 02:32
Show Gist options
  • Save guannie/750313cac27d308f3d9d256d987defd1 to your computer and use it in GitHub Desktop.
Save guannie/750313cac27d308f3d9d256d987defd1 to your computer and use it in GitHub Desktop.
A swipetab reimplementation
<ion-header>
<ion-navbar>
<ion-title>Swipe Tab</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-segment class="SwipedTabs-tabs" >
<ion-segment-button *ngFor='let tab of tabs ; let i = index ' value="IngoreMe" (click)="selectTab(i)"
[ngClass]='{ "SwipedTabs-activeTab" : ( this.SwipedTabsSlider && ( this.SwipedTabsSlider.getActiveIndex() === i || ( tabs.length -1 === i&& this.SwipedTabsSlider.isEnd()))) }' >
{{tab}}
</ion-segment-button>
</ion-segment>
<!-- here is our dynamic line "indicator"-->
<div id='indicator' class="SwipedTabs-indicatorSegment" [ngStyle]="{'width.%': (100/this.tabs.length)}"></div>
<ion-slides #SwipedTabsSlider (ionSlideDrag)="animateIndicator($event)"
(ionSlideWillChange)="updateIndicatorPosition()"
(ionSlideDidChange)="updateIndicatorPosition()"
(pan)="updateIndicatorPosition()"
[pager]="false"
>
<ion-slide>
<h1>Page 1 </h1>
</ion-slide>
<ion-slide>
<h1>Page 2 </h1>
</ion-slide>
<ion-slide>
<h1>Page 3 </h1>
</ion-slide>
<ion-slide>
<h1>Page 4 </h1>
</ion-slide>
</ion-slides>
</ion-content>
page-swipetab {
.SwipedTabs-indicatorSegment
{
-webkit-transition: 0.3s all;
-moz-transition: 0.3s all;
-o-transition: 0.3s all;
transition: 0.3s all;
transform-origin: top 0 left 0;
height: 1px;
position: relative;
top: -2px;
background-color: black !important;
}
.SwipedTabs-tabs ion-segment-button
{
border:none !important;
font-size: 20px;
color:gray!important;
background-color:white!important;
}
.SwipedTabs-tabs ion-segment-button.SwipedTabs-activeTab
{
color:black !important;
}
.SwipedTabs-tabs
{
border-bottom: solid 1px #e6e6e6 !important;
height: 48px;
}
}
import { Component, ViewChild } from '@angular/core';
import { NavController, Platform, ActionSheetController, Slides } from 'ionic-angular';
/**
* Generated class for the AbcPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@Component({
selector: 'page-swipetab',
templateUrl: 'swipetab.html',
})
export class SwipeTabPage {
@ViewChild('SwipedTabsSlider') SwipedTabsSlider: Slides ;
SwipedTabsIndicator :any= null;
tabs:any=[];
constructor(public navCtrl: NavController,
public platform: Platform,
public actionsheetCtrl: ActionSheetController
) {
this.tabs=["page1","page2","page3","page4"];
}
ionViewDidEnter() {
this.SwipedTabsIndicator = document.getElementById("indicator");
}
selectTab(index) {
this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(100*index)+'%,0,0)';
this.SwipedTabsSlider.slideTo(index, 500);
}
updateIndicatorPosition() {
// this condition is to avoid passing to incorrect index
if( this.SwipedTabsSlider.length()> this.SwipedTabsSlider.getActiveIndex())
{
this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(this.SwipedTabsSlider.getActiveIndex() * 100)+'%,0,0)';
}
}
animateIndicator($event) {
if(this.SwipedTabsIndicator)
this.SwipedTabsIndicator.style.webkitTransform = 'translate3d(' + (($event.progress* (this.SwipedTabsSlider.length()-1))*100) + '%,0,0)';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment