feat: add pan effect
This commit is contained in:
parent
fbe7481edc
commit
f37eee66c8
66
index.html
66
index.html
|
@ -18,9 +18,12 @@
|
|||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
border: 1px solid black;
|
||||
border: 1px solid white;
|
||||
padding: 24px 48px;
|
||||
gap: 24px;
|
||||
|
||||
transition-property: transform;
|
||||
transition-duration: 100ms;
|
||||
}
|
||||
|
||||
#logo-container {
|
||||
|
@ -41,12 +44,19 @@
|
|||
.reverse {
|
||||
transform: scaleX(-1);
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
#logo {
|
||||
border: 1px solid black;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Vite + TS</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="logo-container">
|
||||
<div id="pan-container" style="padding: 20px">
|
||||
<div id="logo">
|
||||
<div id="brand-reversed" class="brand-name reverse">
|
||||
<p>A</p>
|
||||
|
@ -67,5 +77,59 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
let constrain = 600;
|
||||
let panContainer = document.getElementById("pan-container");
|
||||
let ex1Layer = document.getElementById("logo");
|
||||
|
||||
function transforms(x, y, el) {
|
||||
let box = el.getBoundingClientRect();
|
||||
let calcX = -(y - box.y - box.height / 2) / constrain;
|
||||
let calcY = (x - box.x - box.width / 2) / constrain;
|
||||
|
||||
return (
|
||||
"perspective(100px) " +
|
||||
" rotateX(" +
|
||||
calcX +
|
||||
"deg) " +
|
||||
" rotateY(" +
|
||||
calcY +
|
||||
"deg) "
|
||||
);
|
||||
}
|
||||
|
||||
function resetTransform(x, y, el) {
|
||||
return (
|
||||
"perspective(100px) " + " rotateX(0deg) " + " rotateY(0deg) "
|
||||
);
|
||||
}
|
||||
|
||||
function transformElement(el, xyEl) {
|
||||
el.style.transform = transforms.apply(null, xyEl);
|
||||
}
|
||||
|
||||
function resetElement(el, xyEl) {
|
||||
el.style.transform = resetTransform.apply(null, xyEl);
|
||||
}
|
||||
|
||||
panContainer.onmousemove = function (e) {
|
||||
let xy = [e.clientX, e.clientY];
|
||||
let position = xy.concat([ex1Layer]);
|
||||
|
||||
window.requestAnimationFrame(function () {
|
||||
transformElement(ex1Layer, position);
|
||||
});
|
||||
};
|
||||
|
||||
panContainer.onmouseleave = function (e) {
|
||||
let xy = [0, 0];
|
||||
let position = xy.concat([ex1Layer]);
|
||||
|
||||
window.requestAnimationFrame(function () {
|
||||
resetElement(ex1Layer, position);
|
||||
});
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue
Block a user