forked from lnbiuc/blog-next-view
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ayanamirei
committed
Feb 13, 2024
1 parent
2514d8e
commit 712b3bb
Showing
5 changed files
with
61 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export function addHoverEffect(select: string, offset: number) { | ||
const card: HTMLElement | null = document.querySelector(select) | ||
const motionMatchMedia = window.matchMedia('(prefers-reduced-motion)') | ||
const THRESHOLD = offset | ||
|
||
function handleHover(e: any) { | ||
if (!card) | ||
return | ||
const { clientX, clientY, currentTarget } = e | ||
const { clientWidth, clientHeight, offsetLeft, offsetTop } = currentTarget | ||
|
||
const horizontal = (clientX - offsetLeft) / clientWidth | ||
const vertical = (clientY - offsetTop) / clientHeight | ||
const rotateX = (THRESHOLD / 2 - horizontal * THRESHOLD).toFixed(2) | ||
const rotateY = (vertical * THRESHOLD - THRESHOLD / 2).toFixed(2) | ||
|
||
card.style.transform = `perspective(${clientWidth}px) rotateX(${rotateY}deg) rotateY(${rotateX}deg) scale3d(1, 1, 1)` | ||
} | ||
|
||
function resetStyles(e: any) { | ||
if (!card) | ||
return | ||
card.style.transform = `perspective(${e.currentTarget.clientWidth}px) rotateX(0deg) rotateY(0deg)` | ||
} | ||
|
||
if (!motionMatchMedia.matches) { | ||
if (!card) | ||
return | ||
card.addEventListener('mousemove', handleHover) | ||
card.addEventListener('mouseleave', resetStyles) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters