I am trying to create a scroll effect with an image whose rotateX
is something like 70deg
(or any number).
Whenever someone scrolls the image into viewport, the rotateX
of the image has to become 0deg
.
In the same way, if someone scrolls the image out of the viewport, the rotateX
of the image has to become 70deg
again
Here is my code:
let a = 70
function test(){
let image = document.querySelector("img");
let imageTop = image.getBoundingClientRect().top;
let screenpos = window.innerHeight /2
// console.log("test")
if(imageTop < screenpos){
image.style.border = "5px solid green"
// console.log(window.scrollY/10)
image.style.transform = `rotateX(${a=a-2}deg)`
// console.log("its reached ")
}
}
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;
/* border: 1px solid #000; */
}
<!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>