flowaaa 9 달 전
부모
커밋
a118f4c86e

+ 12 - 30
app-angular/src/modules/lesson/community/community.component.html

@@ -5,7 +5,6 @@
             <ion-icon name="add-circle-outline" size="large" color="dark"></ion-icon>
             <ion-note>发布</ion-note>
         </ion-button>
-        
     </ion-toolbar>
 </ion-header>
 
@@ -40,7 +39,7 @@
                                     {{attention?.get('name')}}
                                 </ion-card-title>
                                 <ion-card-subtitle style="font-size:0.7rem;">
-                                    作者:{{attention?.get('user')?.get('name')||"无名"}}
+                                    作者:{{attention?.get('user')?.get('name') || "无名"}}
                                 </ion-card-subtitle>
                             </ion-card-header>
                             <div class="metadata-end-wrapper">
@@ -56,34 +55,17 @@
         </ion-grid>
     </ng-container>
 
-
     <!-- 关注 -->
     <ng-container *ngIf="cate=='关注'">
-        <ion-grid>
-            <ion-row>
-                <ng-container *ngFor="let attention of attentionList">
-                    <ion-col size="6">
-                        <ion-card class="roleCard" (click)="goAttentionDetail(attention)">
-                            <img alt="关注图" [src]="attention?.get('img')" />
-                            <ion-card-header>
-                                <ion-card-title>
-                                    {{attention?.get('name')}}
-                                </ion-card-title>
-                                <ion-card-subtitle style="font-size:0.7rem;">
-                                    作者:{{attention?.get('user')?.get('name')||"无名"}}
-                                </ion-card-subtitle>
-                            </ion-card-header>
-                            <div class="metadata-end-wrapper">
-                                <ion-button shape="round" size="small" color="danger">
-                                    <ion-icon name="heart-outline" size="small"></ion-icon>
-                                    {{attention?.get('like')}}
-                                </ion-button>
-                            </div>
-                        </ion-card>
-                    </ion-col>
-                </ng-container>
-            </ion-row>
-        </ion-grid>
+        <div class="carousel-container">
+            <div class="carousel">
+                <div class="card">Card 1</div>
+                <div class="card">Card 2</div>
+                <div class="card">Card 3</div>
+                <div class="card">Card 4</div>
+                <div class="card">Card 5</div>
+            </div>
+        </div>
     </ng-container>
 
     <!-- 科普 -->
@@ -93,7 +75,7 @@
                 <img alt="科普图" [src]="science?.get('img')" />
                 <ion-card-header>
                     <ion-card-title>{{science?.get('name')}}</ion-card-title>
-                    <ion-card-subtitle>{{science?.get('createdAt')| date: 'YYYY/MM/dd'||"发布时间"}}</ion-card-subtitle>
+                    <ion-card-subtitle>{{science?.get('createdAt') | date: 'YYYY/MM/dd' || "发布时间"}}</ion-card-subtitle>
                     <div class="metadata-end-wrapper">
                         <ion-button fill="clear" color="dark" size="small" class="see">
                             <ion-icon name="eye-outline" size="small"></ion-icon>
@@ -112,4 +94,4 @@
         </ng-container>
     </ng-container>
 
-</ion-content>
+</ion-content>

+ 43 - 14
app-angular/src/modules/lesson/community/community.component.scss

@@ -1,28 +1,57 @@
 ion-content {
-    height: calc(100vh - 121px) !important;
+  height: calc(100vh - 121px) !important;
 }
 
 .foodContent {
-    display: -webkit-box;
-    -webkit-box-orient: vertical;
-    -webkit-line-clamp: 2;
-    overflow: hidden;
-    text-overflow: ellipsis;
+  display: -webkit-box;
+  -webkit-box-orient: vertical;
+  -webkit-line-clamp: 2;
+  overflow: hidden;
+  text-overflow: ellipsis;
 }
 
 .roleCard {
-    margin: 0px;
+  margin: 0;
 }
 
 .metadata-end-wrapper {
-    position: absolute;
+  position: absolute;
+  top: 3px;
+  inset-inline-end: 3px;
+  font-size: 0.6rem;
+  display: flex;
+  align-items: center;
+}
 
-    top: 3px;
-    inset-inline-end: 3px;
+.carousel-container {
+  width: 100%;
+  overflow: hidden;
+}
 
-    font-size: 0.6rem;
+.carousel {
+  display: flex;
+  overflow-x: auto;
+  scroll-behavior: smooth;
+  scrollbar-width: none; /* 隐藏滚动条 Firefox */
+  -ms-overflow-style: none; /* 隐藏滚动条 IE/Edge */
+  -webkit-overflow-scrolling: touch; /* 兼容iOS的滚动效果 */
+}
 
-    display: flex;
-    align-items: center;
+.carousel::-webkit-scrollbar {
+  display: none; /* 隐藏滚动条 Chrome/Safari */
+}
 
-}
+.card {
+  flex: 0 0 auto;
+  width: 200px;
+  height: 200px;
+  margin-right: 10px;
+  background-color: #f9f9f9;
+  border-radius: 5px;
+  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+}
+
+.carousel-content {
+  display: flex;
+  flex-wrap: nowrap; /* 防止换行 */
+}

+ 59 - 29
app-angular/src/modules/lesson/community/community.component.ts

@@ -1,72 +1,102 @@
-import { Component } from '@angular/core';
-
-// 引用服务
+import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
 import { Router } from '@angular/router';
-
-// 引入Parse第三方库
-import * as Parse from "parse"
-(Parse as any).serverURL = "http://web2023.fmode.cn:9999/parse"
-Parse.initialize("dev")
+import * as Parse from "parse";
 
 @Component({
   selector: 'app-community',
   templateUrl: './community.component.html',
   styleUrls: ['./community.component.scss']
 })
-export class CommunityComponent {
+export class CommunityComponent implements OnInit {
+
+  scienceList: Array<Parse.Object> = [];
+  recommendList: Array<Parse.Object> = [];
+  attentionList: Array<Parse.Object> = [];
+
+  startX: number = 0;
+  currentX: number = 0;
+  isDragging: boolean = false;
+  minTranslateX: number = 0; // 最小平移距离
+  maxTranslateX: number = 0;
 
-  scienceList: Array<Parse.Object> = []
-  recommendList: Array<Parse.Object> = []
-  attentionList: Array<Parse.Object> = []
+  @ViewChild('carousel', { static: true }) carousel!: ElementRef;
 
+  cate: string = 'all';
 
-  // 依赖注入
-  constructor(private router: Router) {
+  constructor(private router: Router, private renderer: Renderer2) {
     this.initPage();
   }
-  // 首次进入页面,默认加载首批数据
+
+  ngOnInit(): void {
+    this.renderer.listen(this.carousel.nativeElement, 'touchstart', this.onTouchStart.bind(this));
+    this.renderer.listen(this.carousel.nativeElement, 'touchmove', this.onTouchMove.bind(this));
+    this.renderer.listen(this.carousel.nativeElement, 'touchend', this.onTouchEnd.bind(this));
+  }
+
   async initPage() {
-    this.scienceList = await this.getScienceData()
-    this.recommendList = await this.gteRecommendData()
-    this.attentionList = await this.gteAttentionData()
+    this.scienceList = await this.getScienceData();
+    this.recommendList = await this.getRecommendData();
+    this.attentionList = await this.getAttentionData();
+    // 设置最大最小平移距离
+    this.maxTranslateX = this.carousel.nativeElement.offsetWidth - window.innerWidth;
   }
 
-  // 数据加载相关函数
   async getScienceData() {
     let query = new Parse.Query("PetScience");
     let list = await query.find();
-    return list
+    return list;
   }
-  async gteRecommendData() {
+
+  async getRecommendData() {
     let query = new Parse.Query("PetRecommend");
     let list = await query.find();
-    return list
+    return list;
   }
-  async gteAttentionData() {
+
+  async getAttentionData() {
     let query = new Parse.Query("PetAttention");
     let list = await query.find();
-    return list
+    return list;
   }
 
-  cate: string = "推荐"
-  // 跳转函数
   goScienceDetail(science: Parse.Object) {
     this.router.navigate(["/lesson/community/scienceDetail"], {
       queryParams: science
-    })
+    });
   }
 
   goRecommendDetail(recommend: Parse.Object) {
     this.router.navigate(["/lesson/community/recommendDetail"], {
       queryParams: recommend
-    })
+    });
   }
 
   goAttentionDetail(attention: Parse.Object) {
     this.router.navigate(["/lesson/community/attentionDetail"], {
       queryParams: attention
-    })
+    });
+  }
+
+  onTouchStart(event: TouchEvent): void {
+    this.startX = event.touches[0].clientX;
+    this.isDragging = true;
   }
 
+  onTouchMove(event: TouchEvent): void {
+    if (this.carousel && this.isDragging) {
+        const touch = event.touches[0];
+        const deltaX = touch.clientX - this.startX;
+        this.currentX = this.currentX + deltaX;
+
+        // 限制平移范围在最大最小值之间
+        this.currentX = Math.max(this.minTranslateX, Math.min(this.maxTranslateX, this.currentX));
 
+        this.renderer.setStyle(this.carousel.nativeElement, 'transform', `translateX(${-this.currentX}px)`);
+        this.startX = touch.clientX;
+    }
+}
+
+  onTouchEnd(): void {
+    this.isDragging = false;
+  }
 }