53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
/**
|
|
* Анимация раскрытия текста в карточках специальностей (product-card).
|
|
* На устройствах с курсором (hover) текст показывается при наведении
|
|
* средствами CSS. На сенсорных устройствах hover недоступен, поэтому
|
|
* карточка получает класс product-card--active, когда попадает в поле
|
|
* зрения: текст раскрывается автоматически при прокрутке.
|
|
*/
|
|
export function useProductCardReveal() {
|
|
if (import.meta.server) return;
|
|
|
|
let observer: IntersectionObserver | null = null;
|
|
|
|
const activateCards = () => {
|
|
document.querySelectorAll(".product-card").forEach((card) => {
|
|
card.classList.add("product-card--active");
|
|
});
|
|
};
|
|
|
|
onMounted(async () => {
|
|
await nextTick();
|
|
|
|
// На устройствах с курсором описания раскрываются по hover в CSS —
|
|
// автоматический показ не нужен.
|
|
const noHover = window.matchMedia("(hover: none)").matches;
|
|
if (!noHover) return;
|
|
|
|
if (typeof IntersectionObserver === "undefined") {
|
|
activateCards();
|
|
return;
|
|
}
|
|
|
|
observer = new IntersectionObserver(
|
|
(entries) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
entry.target.classList.add("product-card--active");
|
|
}
|
|
});
|
|
},
|
|
{ threshold: 0.15, rootMargin: "0px 0px -80px 0px" },
|
|
);
|
|
|
|
document.querySelectorAll(".product-card").forEach((card) => {
|
|
observer?.observe(card);
|
|
});
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
observer?.disconnect();
|
|
observer = null;
|
|
});
|
|
}
|