drift-bottle.component_20241224161310.ts 2.9 KB

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