Created
May 11, 2018 09:27
-
-
Save thomascsd/36adafdf701033d1455a378b98247e2d to your computer and use it in GitHub Desktop.
Enable swipe for tab in Angular material
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="md-content" flex md-scroll-y (swipeleft)="swipe(idx, $event.type)" (swiperight)="swipe(idx, $event.type)"> | |
<md-tab-group md-stretch-tabs [(selectedIndex)]="selectedIndex" (selectedIndexChange)="selectChange()"> | |
<md-tab label="Tab 1" (swipeleft)="swipe(1, $event.type)" (swiperight)="swipe(1, $event.type)"> | |
Content 1 | |
</md-tab> | |
<md-tab label="Tab 2" (swipeleft)="swipe(2, $event.type)" (swiperight)="swipe(2, $event.type)">Content 2</md-tab> | |
</md-tab-group> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TabsOverviewExample { | |
selectedIndex: number = 1; | |
selectChange(): void{ | |
console.log("Selected INDEX: " + this.selectedIndex); | |
} | |
SWIPE_ACTION = { LEFT: 'swipeleft', RIGHT: 'swiperight' }; | |
// Action triggered when user swipes | |
swipe(selectedIndex: number, action = this.SWIPE_ACTION.RIGHT) { | |
console.log("swipe"); | |
console.log("number",selectedIndex); | |
console.log("action",action); | |
// Out of range | |
if (this.selectedIndex < 0 || this.selectedIndex > 1 ) return; | |
// Swipe left, next tab | |
if (action === this.SWIPE_ACTION.LEFT) { | |
const isLast = this.selectedIndex === 1; | |
this.selectedIndex = isLast ? 0 : this.selectedIndex + 1; | |
console.log("Swipe right - INDEX: " + this.selectedIndex); | |
} | |
// Swipe right, previous tab | |
if (action === this.SWIPE_ACTION.RIGHT) { | |
const isFirst = this.selectedIndex === 0; | |
this.selectedIndex = isFirst ? 1 : this.selectedIndex - 1; | |
console.log("Swipe left - INDEX: " + this.selectedIndex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment