I have the following html file:
const container = document.querySelector('.container');
const box = document.querySelector('.box');
const light = document.querySelector('.light');
container.addEventListener('mousemove', moveLight);
function moveLight(event) {
const containerRect = container.getBoundingClientRect();
light.style.left = event.clientX - containerRect.left + 'px';
light.style.top = event.clientY - containerRect.top + 'px';
}
// Adjust the light position initially in case of default hover
container.addEventListener('mouseenter', moveLight);
container.addEventListener('mousemove', rotateBox);
function rotateBox(event) {
const containerRect = container.getBoundingClientRect();
const centerX = containerRect.left + containerRect.width / 2;
const centerY = containerRect.top + containerRect.height / 2;
const mouseX = event.clientX;
const mouseY = event.clientY;
const deltaX = mouseX - centerX;
const deltaY = mouseY - centerY;
const maxRotation = 30; // Adjust the maximum rotation angle
const rotateX = -deltaY / centerY * maxRotation; // Negative rotation for bringing closer
const rotateY = -deltaX / centerX * maxRotation; // Negative rotation for bringing closer
box.style.transform = `rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
}
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
position: relative;
width: 600px;
height: 400px;
perspective: 800px;
/* Adjust as needed */
overflow: hidden;
}
.box {
width: 100%;
height: 100%;
background-color: #0d419d;
transition: transform 0.3s ease;
position: relative;
overflow: hidden;
}
.light {
position: absolute;
width: 300px;
height: 300px;
border-radius: 50%;
background-color: #006d32;
/* Adjust opacity as needed */
pointer-events: none;
/* Ensure the light doesn't interfere with mouse events */
transform: translate(-50%, -50%);
display: none;
/* Initially hidden */
z-index: 1;
filter: blur(3rem);
}
.container:hover .light {
display: block;
/* Show the light when hovering over the container */
}
<div class="container">
<div class="box"></div>
<div class="light"></div>
</div>
Which is exhibiting some behavior I’m not sure how to fix. It is rotating but only a part of the box is rotating on hover, while the rest isn’t, changing the shape of the 2d box.
I’d like the entire box to rotate towards the user (so I’m assuming -z direction), so if I hover the top left corner, the top left corner moves a little towards me with the rest of the box acting normal (as one piece).
I’m sure this can be done in pure css, if anyone has that solution, I’d prefer it, something like a transform property with the following maybe?
perspective(700px) rotateX(-0.03deg) rotateY(0.03deg);
2
Answers
I saw the issue as when the .box rotates, it reached the boundaries of the parent element (.container), and the .container cut off the entire visible part of the .box that extended beyond its boundaries, because this is exactly how ‘overflow:hidden’ works.
Your code works perfectly to me and this small adjustment will help you to see it: remove ‘
overflow: hidden;
‘ property from the ‘.container’ element.So leave it styled like this:
Creating a rotating box on hover with a light effect under the cursor involves a combination of CSS for the rotating box and JavaScript for the light effect. Here’s how you can achieve it:
In this code: