강의

멘토링

커뮤니티

Inflearn Community Q&A

caesiumy's profile image
caesiumy

asked

BBC Interactive Page "The Future of Offices: How Covid-19 Will Change Its Ways" Clone

JS part 1

forEach로 작성해봤습니다!

Resolved

Written on

·

275

1

window.addEventListener("scroll", () => {
    let boundingRect;
    let temp = 0;

    steps.forEach((step, i) => {
      if (i > ioIndex + 1 || i < ioIndex - 1) return;
      boundingRect = step.getBoundingClientRect();
      temp++;

      if (
        boundingRect.top > window.innerHeight * 0.2 &&
        boundingRect.top < window.innerHeight * 0.8
      ) {
        inactivate();

        currentItem = graphics[step.dataset.index];
        activate();
      }
    });

    console.log("temp", temp);
  });

말 안 듣고 forEach로 만들어봤는데

temp도 2, 3 뜨는 거 보면 잘 동작하는 거 같습니다만

혹시 틀린 점이 있을까요??

클론코딩인터랙티브-웹

Answer 1

1

studiomeal님의 프로필 이미지
studiomeal
Instructor

for 문으로 할 경우에는 ioIndex - 1 부터 ioIndex + 1 까지만 루프를 도는데,
작성하신 코드는 일단 루프 자체는 전체를 돌면서 if로 ioIndex를 체크하는 차이가 있습니다.
이렇게 하셔도 별 상관은 없습니다^^

내용이랑 별개로, 이렇게 다른 시도를 해보시는 것은 실력 향상에 도움이 많이 된다고 생각합니다^^

caesiumy's profile image
caesiumy

asked

Ask a question