When I use .show
instead of .box.show
in the CSS, the even boxes do not come from the left side. This discrepancy has left me puzzled as I assumed they would have the same effect. However, it appears that in this particular code snippet, they are behaving differently.
const boxes = document.querySelectorAll('.box');
window.addEventListener('scroll',()=>{
const triggerPoint=window.innerHeight*4/5;
boxes.forEach((box)=>{
const boxTop=box.getBoundingClientRect().top;
if(boxTop<triggerPoint){
box.classList.add('show')
}else{
box.classList.remove('show')
}
})
})
*{
padding:0;
margin:0;
box-sizing: border-box;
}
body{
background-color: #efedd6;
min-height: 100%;
width:100%;
display:flex;
justify-content: center;
align-items: center;
flex-direction: column;
overflow-x: hidden;
}
.box{
width: 100px;
height: 100px;
background-color: rgb(226, 43, 43);
margin:10px;
transform: translateX(4000%);
transition:0.4s;
}
h1{
margin:10px;
}
.box:nth-of-type(even){
transform: translateX(-4000%);
}
.box.show{
transform: translateX(0%);
transition: .4s;
}
<!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">
<link rel="stylesheet" href="style.css">
<title>Scroll Animation</title>
</head>
<body>
<!-- <h1>scroll to see the Animation</h1> -->
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<script src="main.js"></script>
</body>
</html>