JSFiddle - React, Tailwind, and code Playground
by metacode
HTML
<div class="container">
<div class="button">Click</div>
</div>
CSS
body {
margin: 0;
}
div {
box-sizing: border-box;
}
div.container {
padding: 30px;
display: inline-block;
position: relative;
z-index: 50;
top: 0;
left: 0;
}
div.button {
width: 100px;
height: 100px;
padding: 8px;
background-clip: content-box;
color: white;
font: normal 20px/84px'open sans', sans-serif;
letter-spacing: 1.2px;
text-align: center;
border-radius: 100px;
box-shadow: 0 0.8px 8px rgba(0, 0, 0, .3);
background-image: -webkit-radial-gradient(center center, circle, #8CE2FB, #52D2F8);
background-image: -moz-radial-gradient(center center, circle, #8CE2FB, #52D2F8);
background-image: -ms-radial-gradient(center center, circle, #8CE2FB, #52D2F8);
background-image: -o-radial-gradient(center center, circle, #8CE2FB, #52D2F8);
background-image: radial-gradient(center center, circle, #8CE2FB, #52D2F8);
transition: all .2s;
-webkit-user-select: none;
-moz-user-select: -moz-none;
-o-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: pointer;
}
JavaScript
(function ($) {
var $container = $('div.container'),
$button = $('div.button');
$container.on('mouseenter', function (e) {
var mouseLocation = {
x: e.pageX,
y: e.pageY
}
var buttonLocation = {
x: $button.offset().left,
y: $button.offset().top
}
var loc = getMovementDirection (mouseLocation, buttonLocation, jQuery);
console.log(loc);
});
})(jQuery);
function getMovementDirection (mouse, container, $) {
var width = $('div.button').outerWidth(),
height = $('div.button').outerHeight();
var x, y;
if (mouse.x < container.x) {
x = -1;
} else if (mouse.x < container.x + width) {
x = 0;
} else {
x = 1;
}
if (mouse.y < container.y) {
y = -1;
} else if (mouse.y < container.y + width) {
y = 0;
} else {
y = 1;
}
if (x === -1 && y === -1) {
return 'se';
} else if (x === 0 && y === -1) {
return 's';
} else if (x === 1 && y === -1) {
return 'sw';
} else if (x === -1 && y === 0) {
return 'e';
}
// x === 0 && y === 0 is forbidden
else if (x === 1 && y === 0) {
return 'w';
} else if (x === -1 && y === 1) {
return 'ne';
} else if (x === 0 && y === 1) {
return 'n';
} else if (x === 1 && y === 1) {
return 'nw';
}
}