feat: add 3D tilt effect and hover functionality to collection cards

This commit is contained in:
zurdi
2025-05-26 12:29:53 +00:00
parent 7424f4ec6d
commit 4ae9c10d76
6 changed files with 147 additions and 72 deletions

View File

@@ -14,6 +14,10 @@ const storedCollections = localStorage.getItem("settings.gridCollections");
const gridCollections = ref(
isNull(storedCollections) ? false : storedCollections === "true",
);
const isHovering = ref(false);
const hoveringCollectionId = ref();
// Functions
function toggleGridCollections() {
gridCollections.value = !gridCollections.value;
localStorage.setItem(
@@ -21,6 +25,11 @@ function toggleGridCollections() {
gridCollections.value.toString(),
);
}
function onHover(emitData: { isHovering: boolean; id: number }) {
isHovering.value = emitData.isHovering;
hoveringCollectionId.value = emitData.id;
}
</script>
<template>
<r-section icon="mdi-bookmark-box-multiple" :title="t('common.collections')">
@@ -47,12 +56,16 @@ function toggleGridCollections() {
<v-col
v-for="collection in collections.allCollections"
:key="collection.name"
class="pa-1"
class="pa-1 my-4"
:cols="views[0]['size-cols']"
:sm="views[0]['size-sm']"
:md="views[0]['size-md']"
:lg="views[0]['size-lg']"
:xl="views[0]['size-xl']"
:style="{
zIndex:
isHovering && hoveringCollectionId === collection.id ? 1100 : 1,
}"
>
<collection-card
show-rom-count
@@ -61,6 +74,8 @@ function toggleGridCollections() {
:collection="collection"
with-link
title-on-hover
enable3DTilt
@hover="onHover"
/>
</v-col>
</v-row>

View File

@@ -72,7 +72,7 @@ function onClosedMenu() {
<v-col
v-for="rom in continuePlayingRoms"
:key="rom.id"
class="pa-1 align-self-end mt-4 mb-8"
class="pa-1 align-self-end my-4"
:cols="views[0]['size-cols']"
:sm="views[0]['size-sm']"
:md="views[0]['size-md']"

View File

@@ -57,7 +57,7 @@ function onHover(emitData: { isHovering: boolean; id: number }) {
<v-col
v-for="platform in filledPlatforms"
:key="platform.slug"
class="pa-1 mt-4 mb-8"
class="pa-1 my-4"
:cols="views[0]['size-cols']"
:sm="views[0]['size-sm']"
:md="views[0]['size-md']"

View File

@@ -70,7 +70,7 @@ function onClosedMenu() {
<v-col
v-for="rom in recentRoms"
:key="rom.id"
class="pa-1 align-self-end mt-4 mb-8"
class="pa-1 align-self-end my-4"
:cols="views[0]['size-cols']"
:sm="views[0]['size-sm']"
:md="views[0]['size-md']"

View File

@@ -18,6 +18,11 @@ const gridVirtualCollections = ref(
? false
: storedVirtualCollections === "true",
);
const visibleCollections = ref(72);
const isHovering = ref(false);
const hoveringCollectionId = ref();
// Functions
function toggleGridVirtualCollections() {
gridVirtualCollections.value = !gridVirtualCollections.value;
localStorage.setItem(
@@ -25,9 +30,12 @@ function toggleGridVirtualCollections() {
gridVirtualCollections.value.toString(),
);
}
const visibleCollections = ref(72);
// Functions
function onHover(emitData: { isHovering: boolean; id: number }) {
isHovering.value = emitData.isHovering;
hoveringCollectionId.value = emitData.id;
}
function onScroll() {
if (
window.innerHeight + window.scrollY >= document.body.offsetHeight - 60 &&
@@ -72,12 +80,16 @@ onBeforeUnmount(() => {
visibleCollections,
)"
:key="collection.name"
class="pa-1"
class="pa-1 my-4"
:cols="views[0]['size-cols']"
:sm="views[0]['size-sm']"
:md="views[0]['size-md']"
:lg="views[0]['size-lg']"
:xl="views[0]['size-xl']"
:style="{
zIndex:
isHovering && hoveringCollectionId === collection.id ? 1100 : 1,
}"
>
<collection-card
show-rom-count
@@ -86,6 +98,8 @@ onBeforeUnmount(() => {
:key="collection.id"
:collection="collection"
with-link
enable3DTilt
@hover="onHover"
/>
</v-col>
</v-row>

View File

@@ -1,9 +1,10 @@
<script setup lang="ts">
import type { Collection, VirtualCollection } from "@/stores/collections";
import storeGalleryView from "@/stores/galleryView";
import storeCollections from "@/stores/collections";
import { ROUTES } from "@/plugins/router";
import { computed, ref, watchEffect } from "vue";
import { computed, ref, watchEffect, onMounted, onBeforeUnmount } from "vue";
import { useDisplay } from "vuetify";
import VanillaTilt from "vanilla-tilt";
import { getCollectionCoverImage, getFavoriteCoverImage } from "@/utils/covers";
// Props
@@ -15,6 +16,7 @@ const props = withDefaults(
titleOnHover?: boolean;
showRomCount?: boolean;
withLink?: boolean;
enable3DTilt?: boolean;
src?: string;
}>(),
{
@@ -23,12 +25,13 @@ const props = withDefaults(
titleOnHover: false,
showRomCount: false,
withLink: false,
enable3DTilt: false,
src: "",
},
);
const { smAndDown } = useDisplay();
const galleryViewStore = storeGalleryView();
const collectionsStore = storeCollections();
const memoizedCovers = ref({
large: ["", ""],
@@ -92,80 +95,123 @@ const firstCover = computed(() => memoizedCovers.value.large[0]);
const secondCover = computed(() => memoizedCovers.value.large[1]);
const firstSmallCover = computed(() => memoizedCovers.value.small[0]);
const secondSmallCover = computed(() => memoizedCovers.value.small[1]);
// Tilt 3D effect logic
interface TiltHTMLElement extends HTMLElement {
vanillaTilt?: {
destroy: () => void;
};
}
const emit = defineEmits(["hover"]);
const tiltCard = ref<TiltHTMLElement | null>(null);
onMounted(() => {
if (tiltCard.value && !smAndDown.value && props.enable3DTilt) {
VanillaTilt.init(tiltCard.value, {
max: 20,
speed: 400,
scale: 1.1,
});
}
});
onBeforeUnmount(() => {
if (tiltCard.value?.vanillaTilt && props.enable3DTilt) {
tiltCard.value.vanillaTilt.destroy();
}
});
</script>
<template>
<v-hover v-slot="{ isHovering, props: hoverProps }">
<v-card
v-bind="{
...hoverProps,
...(withLink && collection
? {
to: {
name: ROUTES.COLLECTION,
params: { collection: collection.id },
},
}
: {}),
}"
:class="{
'on-hover': isHovering,
'transform-scale': transformScale,
}"
:elevation="isHovering && transformScale ? 20 : 3"
>
<v-row v-if="showTitle" class="pa-1 justify-center bg-surface">
<div data-tilt ref="tiltCard">
<v-card
v-bind="{
...hoverProps,
...(withLink && collection
? {
to: {
name: ROUTES.COLLECTION,
params: { collection: collection.id },
},
}
: {}),
}"
:class="{
'on-hover': isHovering,
'transform-scale': transformScale,
}"
:elevation="isHovering && transformScale ? 20 : 3"
:aria-label="`${collection.name} game card`"
@mouseenter="
() => {
emit('hover', { isHovering: true, id: collection.id });
}
"
@mouseleave="
() => {
emit('hover', { isHovering: false, id: collection.id });
}
"
>
<v-row v-if="showTitle" class="pa-1 justify-center bg-surface">
<div
:title="collection.name?.toString()"
class="py-4 px-6 text-truncate text-caption"
>
<span>{{ collection.name }}</span>
</div>
</v-row>
<div
:title="collection.name?.toString()"
class="py-4 px-6 text-truncate text-caption"
class="image-container"
:style="{
aspectRatio: galleryViewStore.defaultAspectRatioCollection,
}"
>
<span>{{ collection.name }}</span>
</div>
</v-row>
<div
class="image-container"
:style="{ aspectRatio: galleryViewStore.defaultAspectRatioCollection }"
>
<template v-if="collection.is_virtual || !collection.path_cover_large">
<div class="split-image first-image">
<template
v-if="collection.is_virtual || !collection.path_cover_large"
>
<div class="split-image first-image">
<v-img
cover
:src="firstCover"
:lazy-src="firstSmallCover"
:aspect-ratio="galleryViewStore.defaultAspectRatioCollection"
/>
</div>
<div class="split-image second-image">
<v-img
cover
:src="secondCover"
:lazy-src="secondSmallCover"
:aspect-ratio="galleryViewStore.defaultAspectRatioCollection"
/>
</div>
</template>
<template v-else>
<v-img
cover
:src="firstCover"
:lazy-src="firstSmallCover"
:src="src || collection.path_cover_large"
:lazy-src="src || collection.path_cover_small?.toString()"
:aspect-ratio="galleryViewStore.defaultAspectRatioCollection"
/>
</template>
<div class="position-absolute append-inner">
<slot name="append-inner"></slot>
</div>
<div class="split-image second-image">
<v-img
cover
:src="secondCover"
:lazy-src="secondSmallCover"
:aspect-ratio="galleryViewStore.defaultAspectRatioCollection"
/>
</div>
</template>
<template v-else>
<v-img
cover
:src="src || collection.path_cover_large"
:lazy-src="src || collection.path_cover_small?.toString()"
:aspect-ratio="galleryViewStore.defaultAspectRatioCollection"
/>
</template>
<div class="position-absolute append-inner">
<slot name="append-inner"></slot>
</div>
</div>
<v-chip
v-if="showRomCount"
class="bg-background position-absolute"
size="x-small"
style="bottom: 0.5rem; right: 0.5rem"
label
>
{{ collection.rom_count }}
</v-chip>
</v-card>
<v-chip
v-if="showRomCount"
class="bg-background position-absolute"
size="x-small"
style="bottom: 0.5rem; right: 0.5rem"
label
>
{{ collection.rom_count }}
</v-chip>
</v-card>
</div>
</v-hover>
</template>