Back to Snippets

Direction Aware Hover Overlay JS

Hover a card and the colored overlay enters from the side your cursor came in on. Move away and it leaves toward the side you exited.

The direction comes from the cursor position measured against the card center. Math.atan2 turns that offset into an angle, which is rounded into four quadrants for top, right, bottom and left. The width to height ratio is factored in first, so the quadrants stay correct on cards that are not square.

Before the slide runs, the overlay is snapped to its starting edge with the transition switched off, then a forced reflow through offsetWidth commits that position. Without the reflow the browser batches both changes into one paint and the overlay simply slides from wherever it already was.

199
#javascript
#css
#hover effect
#direction aware
#math
#overlay
#portfolio
#interactive