tab1.page.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
  2. import { IonHeader, IonToolbar, IonTitle, IonContent, IonButton, IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonItem, IonLabel, IonList, IonInput, IonTextarea, IonAvatar } from '@ionic/angular/standalone';
  3. import { ExploreContainerComponent } from '../explore-container/explore-container.component';
  4. import { CommonModule } from '@angular/common';
  5. import { Router } from '@angular/router';
  6. interface Slide {
  7. image: string;
  8. alt: string;
  9. }
  10. @Component({
  11. selector: 'app-tab1',
  12. templateUrl: 'tab1.page.html',
  13. styleUrls: ['tab1.page.scss'],
  14. standalone: true,
  15. imports: [
  16. CommonModule,
  17. IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent,
  18. IonButton, IonCard, IonCardHeader, IonCardTitle, IonCardContent,
  19. IonList, IonItem, IonLabel, IonAvatar, IonInput, IonTextarea
  20. ],
  21. schemas: [CUSTOM_ELEMENTS_SCHEMA],
  22. })
  23. export class Tab1Page {
  24. coaches = [
  25. { name: '教练张', style: '技术细腻', avatar: 'assets/tab1.img/zhang.png' },
  26. { name: '教练李', style: '战术全面', avatar: 'assets/tab1.img/li.png' },
  27. { name: '教练王', style: '体能训练', avatar: 'assets/tab1.img/wang.png' },
  28. ];
  29. slides: Slide[] = [
  30. { image: 'assets/tab1.img/1.png', alt: '轮播图1' },
  31. { image: 'assets/tab1.img/2.jpg', alt: '轮播图2' },
  32. { image: 'assets/tab1.img/3.jpg', alt: '轮播图3' },
  33. { image: 'assets/tab1.img/4.jpg', alt: '轮播图4' },
  34. { image: 'assets/tab1.img/5.jpg', alt: '轮播图5' }
  35. ];
  36. constructor(
  37. private router: Router,
  38. ) {
  39. }
  40. currentIndex = 0;
  41. translateX = 0;
  42. private autoPlayInterval: any;
  43. private touchStartX = 0;
  44. private touchDeltaX = 0;
  45. private slideWidth = 0;
  46. ngOnInit() {
  47. this.startAutoPlay();
  48. // 获取轮播图容器宽度
  49. setTimeout(() => {
  50. const container = document.querySelector('.banner-container');
  51. if (container) {
  52. this.slideWidth = container.clientWidth;
  53. }
  54. });
  55. }
  56. selectedTag = 'all';
  57. isModalOpen = false;
  58. newPost = {
  59. content: ''
  60. };
  61. trainingPosts = [
  62. {
  63. id: 1,
  64. authorName: '篮球爱好者',
  65. authorAvatar: 'assets/avatars/default-avatar.png',
  66. postTime: new Date(),
  67. content: '今天练习了两小时的投篮,感觉手感越来越好了!',
  68. likes: 12,
  69. comments: 3,
  70. isLiked: false
  71. },
  72. {
  73. id: 2,
  74. authorName: '训练达人',
  75. authorAvatar: 'assets/avatars/default-avatar.png',
  76. postTime: new Date(),
  77. content: '分享一个实用的运球技巧:交叉步过人时重心要放低...',
  78. likes: 18,
  79. comments: 5,
  80. isLiked: false
  81. }
  82. ];
  83. createNewPost() {
  84. this.isModalOpen = true;
  85. }
  86. closeModal() {
  87. this.isModalOpen = false;
  88. this.newPost.content = '';
  89. }
  90. submitPost() {
  91. if (this.newPost.content.trim()) {
  92. const post = {
  93. id: this.trainingPosts.length + 1,
  94. authorName: '当前用户',
  95. authorAvatar: 'assets/avatars/default-avatar.png',
  96. postTime: new Date(),
  97. content: this.newPost.content,
  98. likes: 0,
  99. comments: 0,
  100. isLiked: false
  101. };
  102. this.trainingPosts.unshift(post);
  103. this.closeModal();
  104. }
  105. }
  106. likePost(post: any) {
  107. post.isLiked = !post.isLiked;
  108. post.likes += post.isLiked ? 1 : -1;
  109. }
  110. showComments(post: any) {
  111. // 实现查看评论功能
  112. console.log('查看评论:', post.id);
  113. }
  114. private startAutoPlay() {
  115. this.stopAutoPlay();
  116. this.autoPlayInterval = setInterval(() => {
  117. this.nextSlide();
  118. }, 3000); // 每3秒切换一次
  119. }
  120. private stopAutoPlay() {
  121. if (this.autoPlayInterval) {
  122. clearInterval(this.autoPlayInterval);
  123. }
  124. }
  125. nextSlide() {
  126. this.currentIndex = (this.currentIndex + 1) % this.slides.length;
  127. this.updateTranslateX();
  128. }
  129. prevSlide() {
  130. this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
  131. this.updateTranslateX();
  132. }
  133. goToSlide(index: number) {
  134. this.currentIndex = index;
  135. this.updateTranslateX();
  136. }
  137. private updateTranslateX() {
  138. this.translateX = -this.currentIndex * this.slideWidth;
  139. }
  140. // 触摸事件处理
  141. onTouchStart(event: TouchEvent) {
  142. this.stopAutoPlay();
  143. this.touchStartX = event.touches[0].clientX;
  144. this.touchDeltaX = 0;
  145. }
  146. onTouchMove(event: TouchEvent) {
  147. this.touchDeltaX = event.touches[0].clientX - this.touchStartX;
  148. const newTranslateX = -this.currentIndex * this.slideWidth + this.touchDeltaX;
  149. this.translateX = newTranslateX;
  150. }
  151. onTouchEnd() {
  152. const threshold = this.slideWidth / 3;
  153. if (Math.abs(this.touchDeltaX) > threshold) {
  154. if (this.touchDeltaX > 0) {
  155. this.prevSlide();
  156. } else {
  157. this.nextSlide();
  158. }
  159. } else {
  160. this.updateTranslateX();
  161. }
  162. this.startAutoPlay();
  163. }
  164. goToBasicTraining() {
  165. this.router.navigate(['tabs/basic-training']);
  166. }
  167. }