drift-bottle.component_20241224161152.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { IonHeader, IonButton, IonContent, IonIcon, IonItem, IonLabel, IonList,
  3. IonListHeader, IonCardHeader, IonCardTitle, IonAvatar, IonCardContent, IonTitle, IonCard, IonToolbar, IonInput, IonSearchbar } from '@ionic/angular/standalone';
  4. import { Router } from '@angular/router';
  5. @Component({
  6. selector: 'app-drift-bottle',
  7. template: `
  8. <ion-header>
  9. <ion-toolbar>
  10. <ion-title>Drift Bottle</ion-title>
  11. </ion-toolbar>
  12. </ion-header>
  13. <ion-content [fullscreen]="true">
  14. <div class="top-image" (click)="addRipple($event)">
  15. <img src="/assets/img/漂流瓶.jpg" alt="漂流瓶">
  16. </div>
  17. <!-- 其他内容可以放在这里 -->
  18. <!-- 左边的按钮 -->
  19. <div class="button-container left-button">
  20. <img src="/assets/img/扔一个.png" alt="扔漂流瓶" class="circular-button">
  21. </div>
  22. <!-- 右边的按钮 -->
  23. <div class="button-container right-button">
  24. <img src="/assets/img/捞一个.png" alt="捞漂流瓶" class="circular-button">
  25. </div>
  26. <!-- 背景音乐 -->
  27. <audio #backgroundAudio id="backgroundAudio" autoplay loop>
  28. <source src="/assets/audio/海浪.mp3" type="audio/mpeg">
  29. Your browser does not support the audio element.
  30. </audio>
  31. </ion-content>
  32. `,
  33. styleUrls: ['./drift-bottle.component.scss'],
  34. standalone: true,
  35. imports: [IonHeader, IonToolbar, IonTitle, IonContent,
  36. IonList, IonListHeader, IonItem, IonCardTitle,
  37. IonLabel, IonIcon, IonButton, IonCardContent, IonAvatar,
  38. IonInput, IonSearchbar, IonCard, IonCardHeader
  39. ]
  40. })
  41. export class DriftBottleComponent implements OnInit, OnDestroy {
  42. constructor(private router: Router) { }
  43. ngOnInit(): void {
  44. const audioElement = document.getElementById('backgroundAudio') as HTMLAudioElement;
  45. if (audioElement) {
  46. audioElement.play();
  47. }
  48. }
  49. ngOnDestroy(): void {
  50. const audioElement = document.getElementById('backgroundAudio') as HTMLAudioElement;
  51. if (audioElement) {
  52. audioElement.pause();
  53. audioElement.currentTime = 0; // Reset the audio to the beginning
  54. }
  55. }
  56. goTab1(): void {
  57. this.router.navigate(['tabs/tab1']);
  58. }
  59. addRipple(event: MouseEvent): void {
  60. const container = event.currentTarget as HTMLElement;
  61. const rect = container.getBoundingClientRect();
  62. const x = event.clientX - rect.left;
  63. const y = event.clientY - rect.top;
  64. // 创建涟漪元素
  65. const ripple = document.createElement('span');
  66. ripple.classList.add('ripple');
  67. // 设置涟漪的位置和大小
  68. ripple.style.left = `${x}px`;
  69. ripple.style.top = `${y}px`;
  70. ripple.style.width = ripple.style.height = '100px'; // 可根据需要调整
  71. // 添加到DOM并触发动画
  72. container.appendChild(ripple);
  73. // 动画结束后移除涟漪元素
  74. setTimeout(() => {
  75. ripple.remove();
  76. }, 600); // 应与CSS动画时间相匹配
  77. }
  78. }