JSFiddle - React, Tailwind, and code Playground

HTML

<div class="box-container">
    <div class="box">
        <div class="front"></div>
        <div class="back"></div>
    </div>
    <div class="box">
        <div class="front"></div>
        <div class="back"></div>
    </div>
    <div class="box">
        <div class="front"></div>
        <div class="back"></div>
    </div>
    <div class="box">
        <div class="front"></div>
        <div class="back"></div>
    </div>
</div>

SCSS

* {
    box-sizing: border-box;
}

.box-container {
    padding: 20px;
    &:after {
        content: '';
        display: block;
        clear: both;
    }
    .box {
        float: left;
        width: 50%;
        height: 200px;
        position: relative;
        perspective: 1000px;
        border: 1px solid transparent;
        .front,
        .back {
            float: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            backface-visibility: hidden;
            transition: all .4s ease-in-out;
        }
        .front {
            background: blue;
            transform: rotateX(0) rotateY(0);
            z-index: 900;
        }
        .back {
            background: red;
            z-index: 800;
        }
		
		&:hover {
			// Front hover
			.front {z-index: 900;}

			// Back hover
			.back {z-index: 1000; transform: rotateX(0) rotateY(0);}
		}
		
        // In
        &.hover-in-top {
            .front {
                transform: rotateX(-179deg);
            }
        }
        &.hover-in-right {
            .front {
                transform: rotateY(-179deg);
            }
        }
        &.hover-in-bottom {
            .front {
                transform: rotateX(179deg);
            }
        }
        &.hover-in-left {
            .front {
                transform: rotateY(179deg);
            }
        }
        // Out
        &.hover-out-top {
            .back {
                transform: rotateX(179deg);
            }
        }
        &.hover-out-right {
            .back {
                transform: rotateY(179deg);
            }
        }
        &.hover-out-bottom {
            .back {
                transform: rotateX(-179deg);
            }
        }
        &.hover-out-left {
            .back {
                transform: rotateY(-179deg);
            }
        }
    }
}

JavaScript

$('.box-container .box').each(function() {
    $(this).on('mouseenter mouseleave', function(e) {
        var $this = $(this),
            width = $this.width(),
            height = $this.height();

        var x = (e.pageX - $this.offset().left - (width / 2)) * (width > height ? (height / width) : 1),
            y = (e.pageY - $this.offset().top - (height / 2)) * (height > width ? (width / height) : 1);

        // top = 0, right = 1, bottom = 2, left = 3
        var dir_num = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4,
            directions = ['top', 'right', 'bottom', 'left'];

        // If mouse enter
        if (e.type === 'mouseenter') {
            // Remove all hover out classes
            $this.removeClass(function(index, css) {
                return (css.match(/(^|\s)hover-out-\S+/g) || []).join(' ');
            });

            // Add in direction class
            $this.addClass('hover-in-' + directions[dir_num]);
        }


        // If mouse leave
        if (e.type === 'mouseleave') {
            // Remove all hover in classes
            $this.removeClass(function(index, css) {
                return (css.match(/(^|\s)hover-in-\S+/g) || []).join(' ');
            });

            // Add out direction class
            $this.addClass('hover-out-' + directions[dir_num]);
        }
    });
});