slowly scrolling bug, div stay on page but shouldn't

Viewed 61

I have three div boxes in a row, and on scroll, they should show slowly from the side, but when I scroll up slowly and a little after they show up, the left and right boxes for some reason stay on the page.

My function showBox3 writes in console "false" and classList where is "show" class, but this class shouldn't be there.

Actually, my code works when I scroll normally but when I stop scrolling slightly above the limit, the left and right boxes stay on page.

And one more problem is when "topBox" is a little below the limit, and I scroll up just a little more, but still below the limit, boxes quickly remove from the page and show up again.

const box21 = document.querySelector(".box21");
const box22 = document.querySelector(".box22");
const box23 = document.querySelector(".box23");

var trigger = window.innerHeight * 0.8;
window.addEventListener("scroll", showBox2);

function showBox2() {
  var check;
  const topBox = box22.getBoundingClientRect().top;
  if (trigger > topBox) {
    box22.classList.add("show");
    check = true;
  } else {
    box22.classList.remove("show");
    check = false;
  }
  showBox1(check);
  showBox3(check);
}
function showBox1(ch) {
  if (ch == true) {
    setTimeout(() => {
      box21.classList.add("show");
    }, 400);
  } else {
    box21.classList.remove("show");
  }
}
function showBox3(ch) {
  if (ch == true) {
    setTimeout(function () {
      box23.classList.add("show");
    }, 700);
  } else {
    box23.classList.remove("show");
    console.log(box23.classList);
    console.log(ch);
  }
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

.box1 {
    height: 110vh;
    background-color: aqua;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* I have a bug in box 2 */

.box2 {
    height: 60vh;
    width: 100%;
    background-color: #ddd;
    display: flex;
    align-items: center;
    justify-content: space-around;
}

.box2>div {
    height: 70%;
    width: 27%;
    background-color: black;
}

.box21 {
    transform: translateX(-140%) rotate(-45deg);
    transition: transform 1.4s ease;
}

.box21.show {
    transform: translateX(0%) rotate(0deg);
}


.box22 {
    transform: translateX(340%) rotate(+45deg);
    transition: transform 0.8s ease;
}

.box22.show {
    transform: translateX(0%) rotate(0deg);
}


.box23 {
    transform: translateX(340%) rotate(-45deg);
    transition: transform 1.8s ease;
}

.box23.show {
    transform: translateX(0%) rotate(0deg);
}

/* this part below is not important */
.box3 {
    height: 60vh;
    width: 100%;
    background-color: cornflowerblue;
}
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="css.css" />
  </head>
  <body>
    <div class="box1">
      <h1>Scroll down</h1>
    </div>
    <div class="box2">
      <div class="box21"></div>
      <div class="box22"></div>
      <div class="box23"></div>
    </div>
    <div class="box3"></div>
  </body>
  <script src="js.js"></script>
</html>

0 Answers
Related