JSFiddle - React, Tailwind, and code Playground
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');
function addEvent(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function () {
obj['e' + type + fn](window.event);
}
obj.attachEvent('on' + type, obj[type + fn]);
} else obj.addEventListener(type, fn, false);
}
addEvent( $container[0], 'mouseover', function (e) {
//$container.on('mouseenter', function (e) {
if (e.target === $button[0])
return;
var mouseLocation = {
x: e.pageX,
y: e.pageY
}
var buttonLocation = {
x: $button.offset().left,
y: $button.offset().top,
width: $button.outerWidth(),
height: $button.outerHeight()
}
var loc = getMovementDirection(mouseLocation, buttonLocation, jQuery);
if (loc != undefined) {
var width = $('div.button').outerWidth(),
height = $('div.button').outerHeight();
console.log(mouseLocation.x, mouseLocation.y, buttonLocation.x, buttonLocation.y, width, height);
console.log(loc);
} else {
console.log("wut");
}
});
})(jQuery);
function getMovementDirection(mouse, container, $) {
var x, y;
if (mouse.x < container.x) {
x = -1;
} else if (mouse.x < container.x + container.width) {
x = 0;
} else {
x = 1;
}
if (mouse.y < container.y) {
y = -1;
} else if (mouse.y < container.y + container.height) {
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...