community.component.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import * as Parse from "parse";
  4. @Component({
  5. selector: 'app-community',
  6. templateUrl: './community.component.html',
  7. styleUrls: ['./community.component.scss']
  8. })
  9. export class CommunityComponent implements OnInit {
  10. scienceList: Array<Parse.Object> = [];
  11. recommendList: Array<Parse.Object> = [];
  12. attentionList: Array<Parse.Object> = [];
  13. startX: number = 0;
  14. currentX: number = 0;
  15. isDragging: boolean = false;
  16. minTranslateX: number = 0; // 最小平移距离
  17. maxTranslateX: number = 0;
  18. @ViewChild('carousel', { static: true }) carousel!: ElementRef;
  19. cate: string = 'all';
  20. constructor(private router: Router, private renderer: Renderer2) {
  21. this.initPage();
  22. }
  23. ngOnInit(): void {
  24. this.renderer.listen(this.carousel.nativeElement, 'touchstart', this.onTouchStart.bind(this));
  25. this.renderer.listen(this.carousel.nativeElement, 'touchmove', this.onTouchMove.bind(this));
  26. this.renderer.listen(this.carousel.nativeElement, 'touchend', this.onTouchEnd.bind(this));
  27. }
  28. async initPage() {
  29. this.scienceList = await this.getScienceData();
  30. this.recommendList = await this.getRecommendData();
  31. this.attentionList = await this.getAttentionData();
  32. // 设置最大最小平移距离
  33. this.maxTranslateX = this.carousel.nativeElement.offsetWidth - window.innerWidth;
  34. }
  35. async getScienceData() {
  36. let query = new Parse.Query("PetScience");
  37. let list = await query.find();
  38. return list;
  39. }
  40. async getRecommendData() {
  41. let query = new Parse.Query("PetRecommend");
  42. let list = await query.find();
  43. return list;
  44. }
  45. async getAttentionData() {
  46. let query = new Parse.Query("PetAttention");
  47. let list = await query.find();
  48. return list;
  49. }
  50. goScienceDetail(science: Parse.Object) {
  51. this.router.navigate(["/lesson/community/scienceDetail"], {
  52. queryParams: science
  53. });
  54. }
  55. goRecommendDetail(recommend: Parse.Object) {
  56. this.router.navigate(["/lesson/community/recommendDetail"], {
  57. queryParams: recommend
  58. });
  59. }
  60. goAttentionDetail(attention: Parse.Object) {
  61. this.router.navigate(["/lesson/community/attentionDetail"], {
  62. queryParams: attention
  63. });
  64. }
  65. onTouchStart(event: TouchEvent): void {
  66. this.startX = event.touches[0].clientX;
  67. this.isDragging = true;
  68. }
  69. onTouchMove(event: TouchEvent): void {
  70. if (this.carousel && this.isDragging) {
  71. const touch = event.touches[0];
  72. const deltaX = touch.clientX - this.startX;
  73. this.currentX = this.currentX + deltaX;
  74. // 限制平移范围在最大最小值之间
  75. this.currentX = Math.max(this.minTranslateX, Math.min(this.maxTranslateX, this.currentX));
  76. this.renderer.setStyle(this.carousel.nativeElement, 'transform', `translateX(${-this.currentX}px)`);
  77. this.startX = touch.clientX;
  78. }
  79. }
  80. onTouchEnd(): void {
  81. this.isDragging = false;
  82. }
  83. }