JSFiddle - React, Tailwind, and code Playground
by HDL52
HTML
<div id="element">
<div id="g-img"></div>
</div>
CSS
body {
width: 100%;
height: 100vh;
transform-style: perspective-3d;
perspective: 500px;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
#element {
width: 250px;
height: 350px;
transform-style: perspective-3d;
transition: all 0.1s;
border-radius: 10px;
background: #000;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2), 0 6px 6px rgba(0, 0, 0, 0.2);
border: 3px solid #fff;
}
#g-img {
--per: 30%;
position: relative;
width: 100%;
height: 100%;
background: url('https://raw.githubusercontent.com/Hongda-OSU/PicGo-2.3.1/master/imgicon.png');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
/* filter: brightness(1.1); */
&::after {
content: "";
display: none;
position: absolute;
inset: 0;
border-radius: 10px;
background: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/13471/sparkles.gif");
mix-blend-mode: color-dodge;
}
&::before {
content: "";
display: none;
position: absolute;
inset: 0;
border-radius: 10px;
background:
linear-gradient(115deg,
transparent 0%,
#06e8ff var(--per),
#ffab2e calc(var(--per) + 25%),
#ff2212 calc(var(--per) + 50%),
transparent 100%);
mix-blend-mode: color-dodge;
}
&:hover::after,
&:hover::before {
display: block;
}
}
JavaScript
// https://codepen.io/Chokcoco/pen/yLwOexQ
const multiple = 15;
const mouseOverContainer = document.getElementsByTagName("body")[0];
const element = document.getElementById("element");
const img = document.getElementById("g-img");
function transformElement(x, y) {
let box = element.getBoundingClientRect();
const calcX = -(y - box.y - box.height / 2) / multiple;
const calcY = (x - box.x - box.width / 2) / multiple;
const percentage = parseInt((x - box.x) / box.width * 1000) / 10;
element.style.transform = "rotateX(" + calcX + "deg) " + "rotateY(" + calcY + "deg)";
img.style = `--per: ${percentage}%`;
}
mouseOverContainer.addEventListener("mousemove", (e) => {
window.requestAnimationFrame(function () {
transformElement(e.clientX, e.clientY);
});
});
mouseOverContainer.addEventListener("mouseleave", (e) => {
window.requestAnimationFrame(function () {
element.style.transform = "rotateX(0) rotateY(0)";
});
});