{"id":265970,"date":"2018-02-09T07:24:57","date_gmt":"2018-02-09T14:24:57","guid":{"rendered":"http:\/\/css-tricks.com\/?p=265970"},"modified":"2018-02-09T08:26:55","modified_gmt":"2018-02-09T15:26:55","slug":"direction-aware-hover-effects","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/direction-aware-hover-effects\/","title":{"rendered":"Direction Aware Hover Effects"},"content":{"rendered":"
This is a particular design trick that never fails to catch people’s eye! I don’t know the exact history of who-thought-of-what first and all that, but I know I have seen a number of implementations of it over the years. I figured I’d round a few of them up here.<\/p>\n
<\/p>\n
See the Pen Direction-aware 3D hover effect (Concept)<\/a> by Noel Delgado (@noeldelgado<\/a>) on CodePen<\/a>.<\/p>\n The detection here is done by tracking the mouse position on Then class names are applied depending on that direction to trigger the directional CSS animations. <\/p>\n See the Pen Direction aware hover pure CSS<\/a> by Fabrice Weinberg (@FWeinb<\/a>) on CodePen<\/a>.<\/p>\n Fabrice uses just pure CSS here. They don’t detect the outgoing direction, but they do detect the incoming direction by way of four hidden hoverable boxes, each rotated to cover a triangle. Like this:<\/p>\nmouseover<\/code> and
mouseout<\/code> and calculating which side was crossed. It’s a small amount of clever JavaScript, the meat of which is figuring out that direction:<\/p>\n
var getDirection = function (ev, obj) {\r\n var w = obj.offsetWidth,\r\n h = obj.offsetHeight,\r\n x = (ev.pageX - obj.offsetLeft - (w \/ 2) * (w > h ? (h \/ w) : 1)),\r\n y = (ev.pageY - obj.offsetTop - (h \/ 2) * (h > w ? (w \/ h) : 1)),\r\n d = Math.round( Math.atan2(y, x) \/ 1.57079633 + 5 ) % 4;\r\n \r\n return d;\r\n};<\/code><\/pre>\n
Fabrice Weinberg<\/h3>\n