Looking to achieve a cool scroll effect with an image that rotates on the X-axis by a specific degree, such as 70deg.
The goal is to have the image's rotateX value change to 0deg when it enters the viewport upon scrolling and revert back to 70deg when it exits the viewport.
Below is the code snippet for reference:
let a = 70
function test(){
let image = document.querySelector("img");
let imageTop = image.getBoundingClientRect().top;
let screenpos = window.innerHeight /2
if(imageTop < screenpos){
image.style.border = "5px solid green"
image.style.transform = `rotateX(${a=a-2}deg)`
}
}
window.addEventListener("scroll",function(){
test()
})
body {
background-color: #ccc;
text-align: center;
margin-top: 100px;
font-family: sans-serif;
}
.bgcolor {
background-color: black;
color: rgba(255, 255, 255, 0.8);
}
div{
perspective:800px;
margin-top:400px;
margin-bottom:200px;
}
div img {
transform:rotateX(66deg);
transition:.9s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name=viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Scroll Please</h1>
<div><img src="https://cdn.pixabay.com/photo/2021/06/10/22/14/stork-6327150__340.jpg" alt="Bird Image"></div>
</body>
</html>