Делаю Parallax эффект. Сформировал формулу и написал код для parallax'а. Все работает хорошо, до того момента, пока ты не постигнешь на территорию объекта: при перемещении мышки внутри объекта эффект странно работает.
Код:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<header class="header">
<div class="container">
<ul class="menu">
<a href="#" class="logo">DQVIDKING</a>
<li class="menu-item"><a href="#" class="menu-link">Our Products</a></li>
<li class="menu-item"><a href="#" class="menu-link">About Us</a></li>
<li class="menu-item"><a href="#" class="menu-link">Contacts</a></li>
<li class="menu-item"><a href="#" class="menu-link">FAQ</a></li>
<form action="" class="search">
<input type="search" class="search-area">
<button class="btn search-btn" type="submit">Search</button>
</form>
</ul>
</div>
</header>
<div class="intro">
<div class="container">
<div class="text">
<h1 class="heading">Take your card. Use your uCard</h1>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dignissimos doloremque, fugit neque voluptas adipisci, repellendus nihil eveniet quam laborum fugiat quibusdam blanditiis maiores sed eos.</p>
</div>
<div class="card"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
:root {
--BLACK: #1a1c1f;
--DARK-GRAY: #3a3c3f;
--GRAY: #7a7c7f;
--LIGHT-GRAY: #cacccf;
--WHITE: #fafcff;
--BLUE: #006fff;
}
* {
margin: 0;
padding: 0;
list-style-type: none;
}
a {
text-decoration: none;
padding: 5px;
transition: .2s ease-in-out;
}
a:hover {
color: var(--BLUE);
}
.btn {
border: 1px solid var(--GRAY);
background: transparent;
border-radius: 7px;
padding: 5px 15px;
outline: unset;
transition: .1s ease-in-out;
}
.btn:hover {
background: var(--WHITE);
border-color: var(--BLUE);
border-radius: 10em;
color: var(--BLUE);
border-width: 2px;
padding: 4px 14px;
}
.btn:active {
border-color: transparent;
background: var(--BLACK);
color: var(--WHITE);
border-radius: 0;
}
.container {
width: 1120px;
margin: 0 auto;
}
.header {
background: var(--DARK-GRAY);
padding: 2vh;
}
.menu {
display: flex;
align-items: center;
}
.menu-item {
margin-left: 20px;
}
.menu-link {
font-family: "Fraunces";
color: var(--WHITE);
font-weight: 100;
}
.logo {
font-family: "Montserrat";
color: var(--WHITE);
font-weight: 1000;
}
.search {
margin-left: auto;
}
.search-area {
border: 1px solid var(--GRAY);
border-radius: 10em;
padding: 5px 12px;
outline: unset;
transition: .1s ease-in-out;
}
.search-area:focus {
border-color: var(--BLUE);
border-width: 2px;
padding: 4px 11px;
}
.search-btn {
color: var(--WHITE);
margin-left: 20px;
}
.intro {
background: url(https://images.pexels.com/photos/251225/pexels-photo-251225.jpeg?cs=srgb&dl=pexels-tranmautritam-251225.jpg&fm=jpg);
background-size: cover;
padding: 50px;
height: 79vh;
box-shadow:0 0 0 100vh rgba(0, 0, 0, 0.75) inset;
}
.intro .container {
align-items: center;
display: flex;
perspective: 1000px;
}
.text {
background: var(--DARK-GRAY);
color: var(--WHITE);
height: 125px;
padding: 20px;
width: 500px;
box-shadow: 20px 20px 30px var(--BLACK)
}
.text h1 {
font-family: "Open Sans";
font-weight: 400;
margin-bottom: 20px;
}
.text p {
font-weight: 100;
}
.card {
box-shadow: 20px 20px 30px var(--BLACK);
background: var(--BLUE);
border-radius: 20px;
margin-left: auto;
height: 300px;
width: 500px;
}
JS
(() => {
const card = document.querySelector(".card"),
textBlock = document.querySelector(".text");
let angleY, angleX;
function rotate({layerX, layerY}) {
y = (innerWidth / 360) - (layerX / 180) * 2;
x = (innerHeight / 360) - (layerY / 180) * 2;
card.style.transform = `rotateY(${-y}deg) rotateX(${-x}deg)`;
textBlock.style.transform = `rotateY(${-y}deg) rotateX(${-x}deg)`;
}
window.addEventListener("mousemove", rotate)
card.addEventListener("mousemove", rotate)
})();