tab1.page.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Component, AfterViewInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. @Component({
  4. selector: 'app-tab1',
  5. templateUrl: 'tab1.page.html',
  6. styleUrls: ['tab1.page.scss']
  7. })
  8. export class Tab1Page implements AfterViewInit {
  9. doctors = [
  10. {
  11. avatar: '../../assets/images/doctor.png',
  12. name: '张医生',
  13. specialty: '心血管科'
  14. },
  15. {
  16. avatar: '../../assets/images/doctor.png',
  17. name: '李医生',
  18. specialty: '神经科'
  19. },
  20. {
  21. avatar: '../../assets/images/doctor.png',
  22. name: '王医生',
  23. specialty: '儿科'
  24. },
  25. {
  26. avatar: '../../assets/images/doctor.png',
  27. name: '赵医生',
  28. specialty: '外科'
  29. },
  30. {
  31. avatar: '../../assets/images/doctor.png',
  32. name: '陈医生',
  33. specialty: '内科'
  34. }
  35. ];
  36. // 功能按钮数据
  37. functionItems1 = [
  38. { label: '我的病历', icon: 'document-text', route: '/medical-records' },
  39. { label: '在线问诊', icon: 'chatbubbles', route: '/online-consultation' },
  40. { label: '健康档案', icon: 'person', route: '/health-profile' },
  41. { label: '预约挂号', icon: 'calendar', route: '/appointment' },
  42. { label: '健康资讯', icon: 'newspaper', route: '/health-news' }
  43. ];
  44. functionItems2 = [
  45. { label: '药品购买', icon: 'medkit', route: '/medicine-purchase' },
  46. { label: '体检套餐', icon: 'clipboard', route: '/checkup-packages' },
  47. { label: '专家讲座', icon: 'podium', route: '/expert-lectures' },
  48. { label: '远程医疗', icon: 'videocam', route: '/telemedicine' },
  49. { label: '健康社区', icon: 'people', route: '/health-community' }
  50. ];
  51. currentIndex = 0;
  52. startX: number = 0; // 初始化 startX
  53. endX: number = 0; // 初始化 endX
  54. isDragging: boolean = false; // 标记是否正在拖动
  55. currentTranslate: number = 0; // 当前的平移值
  56. cardWidth = 216; // 每个卡片的宽度加上间距 (200 + 16)
  57. isCyclic: boolean = true; // 是否启用循环滑动
  58. constructor(private router: Router) {}
  59. ngAfterViewInit() {
  60. this.updateCarousel(); // 确保初始状态正确
  61. this.calculateCardWidth(); // 动态计算卡片宽度
  62. }
  63. // 计算卡片宽度
  64. calculateCardWidth() {
  65. const container = document.getElementById('carousel');
  66. if (container && container.children.length > 0) {
  67. const firstCard = container.children[0] as HTMLElement;
  68. this.cardWidth = firstCard.offsetWidth + 16; // 加上间距
  69. }
  70. }
  71. // 更新轮播位置
  72. updateCarousel() {
  73. this.currentTranslate = -this.currentIndex * this.cardWidth;
  74. this.updateCarouselPosition();
  75. }
  76. // 更新轮播位置
  77. updateCarouselPosition() {
  78. const container = document.getElementById('carousel');
  79. if (container) {
  80. container.style.transform = `translateX(${this.currentTranslate}px)`;
  81. }
  82. }
  83. // 触摸开始
  84. onTouchStart(event: TouchEvent) {
  85. // event.preventDefault(); // 防止默认行为
  86. this.startX = event.touches[0].clientX;
  87. this.isDragging = true;
  88. }
  89. // 触摸移动
  90. onTouchMove(event: TouchEvent) {
  91. if (this.isDragging) {
  92. // event.preventDefault(); // 防止默认行为
  93. const touchX = event.touches[0].clientX;
  94. const deltaX = touchX - this.startX;
  95. this.currentTranslate = -this.currentIndex * this.cardWidth + deltaX; // 动态更新平移值
  96. // 限制 currentTranslate 的值
  97. const maxTranslate = -(this.doctors.length - 1) * this.cardWidth;
  98. this.currentTranslate = Math.max(Math.min(this.currentTranslate, 0), maxTranslate);
  99. this.updateCarouselPosition();
  100. }
  101. }
  102. // 触摸结束
  103. onTouchEnd(event: TouchEvent) {
  104. this.endX = event.changedTouches[0].clientX;
  105. this.isDragging = false;
  106. const swipeThreshold = 50; // 滑动阈值
  107. const deltaX = this.startX - this.endX;
  108. if (deltaX > swipeThreshold) {
  109. // 向左滑动
  110. this.nextSlide();
  111. } else if (deltaX < -swipeThreshold) {
  112. // 向右滑动
  113. this.prevSlide();
  114. } else {
  115. // 如果滑动距离不够,则恢复到原来的位置
  116. this.snapToNearestCard();
  117. }
  118. }
  119. // 上一张
  120. prevSlide() {
  121. if (this.isCyclic) {
  122. if (this.currentIndex === 0) {
  123. this.currentIndex = this.doctors.length - 1;
  124. } else {
  125. this.currentIndex--;
  126. }
  127. } else {
  128. if (this.currentIndex > 0) {
  129. this.currentIndex--;
  130. }
  131. }
  132. this.updateCarousel();
  133. }
  134. // 下一张
  135. nextSlide() {
  136. if (this.isCyclic) {
  137. if (this.currentIndex === this.doctors.length - 1) {
  138. this.currentIndex = 0;
  139. } else {
  140. this.currentIndex++;
  141. }
  142. } else {
  143. if (this.currentIndex < this.doctors.length - 1) {
  144. this.currentIndex++;
  145. }
  146. }
  147. this.updateCarousel();
  148. }
  149. // 快照到最近的卡片
  150. snapToNearestCard() {
  151. const cardIndex = Math.round(-this.currentTranslate / this.cardWidth);
  152. this.currentIndex = Math.max(0, Math.min(cardIndex, this.doctors.length - 1));
  153. this.updateCarousel();
  154. }
  155. // 导航到指定路由
  156. navigateTo(route: string) {
  157. this.router.navigate([route]);
  158. }
  159. // 发布求医信息
  160. publishHealthInfo() {
  161. // 这里可以添加发布求医信息的逻辑
  162. console.log('发布求医信息');
  163. }
  164. }