EventCarousel.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="relative">
  3. <div
  4. ref="trackRef"
  5. class="carousel-track"
  6. @scroll.passive="onScroll"
  7. >
  8. <div
  9. v-for="photo in photos"
  10. :key="photo.id"
  11. class="carousel-slide"
  12. >
  13. <img
  14. :src="photo.url ?? ''"
  15. :alt="photo.name ?? 'Foto do evento'"
  16. class="carousel-img"
  17. loading="lazy"
  18. >
  19. </div>
  20. </div>
  21. <template v-if="photos.length > 1">
  22. <button
  23. type="button"
  24. class="carousel-nav left-2"
  25. aria-label="Foto anterior"
  26. @click="go(-1)"
  27. >
  28. <span class="material-icons text-xl">chevron_left</span>
  29. </button>
  30. <button
  31. type="button"
  32. class="carousel-nav right-2"
  33. aria-label="Próxima foto"
  34. @click="go(1)"
  35. >
  36. <span class="material-icons text-xl">chevron_right</span>
  37. </button>
  38. <div class="flex justify-center gap-2 mt-3">
  39. <button
  40. v-for="(photo, index) in photos"
  41. :key="photo.id"
  42. type="button"
  43. class="carousel-dot"
  44. :class="{ 'carousel-dot--active': index === activeIndex }"
  45. :aria-label="`Ir para a foto ${index + 1}`"
  46. @click="goTo(index)"
  47. />
  48. </div>
  49. </template>
  50. </div>
  51. </template>
  52. <script setup lang="ts">
  53. import type { EventMedia } from '~/composables/useEvents'
  54. const { photos } = defineProps<{ photos: EventMedia[] }>()
  55. const trackRef = ref<HTMLElement | null>(null)
  56. const activeIndex = ref(0)
  57. const onScroll = () => {
  58. const track = trackRef.value
  59. if (!track) return
  60. activeIndex.value = Math.round(track.scrollLeft / track.clientWidth)
  61. }
  62. const goTo = (index: number) => {
  63. const track = trackRef.value
  64. if (!track) return
  65. track.scrollTo({ left: index * track.clientWidth, behavior: 'smooth' })
  66. }
  67. const go = (step: number) => {
  68. const total = photos.length
  69. goTo((activeIndex.value + step + total) % total)
  70. }
  71. </script>
  72. <style scoped>
  73. .carousel-track {
  74. display: flex;
  75. overflow-x: auto;
  76. scroll-snap-type: x mandatory;
  77. scrollbar-width: none;
  78. border-radius: 0.75rem;
  79. }
  80. .carousel-track::-webkit-scrollbar {
  81. display: none;
  82. }
  83. .carousel-slide {
  84. flex: 0 0 100%;
  85. scroll-snap-align: start;
  86. }
  87. .carousel-img {
  88. width: 100%;
  89. height: 240px;
  90. object-fit: cover;
  91. display: block;
  92. }
  93. @media (min-width: 1024px) {
  94. .carousel-img {
  95. height: 280px;
  96. }
  97. }
  98. .carousel-nav {
  99. position: absolute;
  100. top: 50%;
  101. transform: translateY(-50%);
  102. display: inline-flex;
  103. align-items: center;
  104. justify-content: center;
  105. width: 36px;
  106. height: 36px;
  107. border-radius: 9999px;
  108. background-color: rgba(255, 255, 255, 0.9);
  109. color: #661d75;
  110. box-shadow: 0 2px 10px rgba(102, 29, 117, 0.18);
  111. transition: background-color 0.2s;
  112. cursor: pointer;
  113. }
  114. .carousel-nav:hover {
  115. background-color: #ffffff;
  116. }
  117. .carousel-dot {
  118. width: 8px;
  119. height: 8px;
  120. border-radius: 9999px;
  121. background-color: #d0b9d4;
  122. transition: background-color 0.2s, width 0.2s;
  123. cursor: pointer;
  124. }
  125. .carousel-dot--active {
  126. width: 20px;
  127. background-color: #661d75;
  128. }
  129. </style>