inflearn logo
강의

Course

Instructor

Getting Started with Programming: Introduction to Web (Inflearn Original)

Creating Instagram Post Cards - JavaScript, Carousel (4-2)

인디케이터 업데이트에서 오류가 발생합니다.

268

loadingband5761

12 asked

0

첫 이미지에서 오른쪽 클릭을 하면 인디케이터가 바뀌고 또 다시 클릭을 하면 에러가 발생하고 바뀌지 않습니다

에러는 Uncaught TypeError: Cannot set property 'className' of undefined

    at updateIndicator (carousel.js:66)

    at HTMLDivElement.<anonymous> (carousel.js:44)

입니다

작성한 코드는 

window.addEventListener('load', function () {
  let carousels = document.getElementsByClassName('carousel');
  //캐러셀 이벤트를 등록하는 로직
  for (let i = 0; i < carousels.length; i++{
    addEventToCarousel(carousels[i]);
  }
});

function addEventToCarousel(carouselElem){
  let ulElem = carouselElem.querySelector('ul');
  let liElems = ulElem.querySelectorAll('li');

  //너비 값 조정
  let liWidth = liElems[0].clientWidth;
  let adjustedWidth = liElems.length * liWidth;
  ulElem.style.width = adjustedWidth + 'px';

  //슬라이드 버튼 이벤트 등록
  let slideButtons = carouselElem.querySelectorAll('.slide');
  for (let i = 0; i < slideButtons.length; i++){
    slideButtons[i].addEventListener('click', createListenerSlide(carouselElem));
  }

}

function createListenerSlide(carouselElem){
  return function(e){
    let clickedButton = event.currentTarget;

    //값 가져오기
    let liElems = carouselElem.querySelectorAll('li');
    let liCount = liElems.length;
    let currentIndex = carouselElem.attributes.data.value;

    //슬라이드 버튼 체크
    if(clickedButton.className.includes('right'&& currentIndex < liCount - 1){
        currentIndex ++;
        scrollDiv(carouselElem, currentIndex);
    } else if (clickedButton.className.includes('left'&& currentIndex > 0){
        currentIndex --;
        scrollDiv(carouselElem, currentIndex);
    }
    //인디케이터 업데이트
    updateIndicator(carouselElem, currentIndex);
    //슬라이드 버튼 보여줌 여부 업데이트

    //새롭게 보여지는 이미지 인덱스 값을 현재 data 값으로 업데이트
    carouselElem.attributes.data.value = currentIndex;
  }
}

function scrollDiv(carouselElem, nextIndex) {
  let scrollable = carouselElem.querySelector('div');
  let liWidth = scrollable.clientWidth;
  let newLeft = liWidth * nextIndex;

  scrollable.scrollTo({left: newLeft, behavior: 'smooth'});
}

function updateIndicator(carouselElem, currentIndex){
  let indicators = carouselElem.querySelectorAll('footer > div');
  for (let i = 0; indicators.length; i++{
    if (currentIndex == i){
      indicators[i].className = 'active';
    } else {
      indicators[i].className = '';
    }
  }
}
여기까지 입니다

javascript HTML/CSS

Answer 1

0

loadingband5761

for (let i = 0; indicators.length; i++{

이 부분에서 i < indicators를 뺴먹었네요,, 해결했습니당

unregistered 이라 뜨며 작동하지 않습니다.

0

56

1

공부한 내용을 블로그에 정리해서 올려도되나요?

0

97

1

sublime text 설치 불가

0

177

1

사진과 본문 내용에 패딩이 40px 들어가 있는데 이유를 모르겠습니다

0

102

1

처음 질문과 옵션 관련 질문입니다.

0

90

1

마지막에 news 관련 질문

0

85

1

태그 닫는 단축키가 뭔가요?

0

171

1

강의에 사용하는 예제 코드 자료 있나요?

0

389

1

회원가입 폼 검증하기 실행이 안됩니다.

0

279

1

이미지랑 아이콘 파일을 어떻게 찾아요?

0

357

1

VS 코드는 절대 경로랑 상대 경로로 상위 문서 접근이 차단되어 있네요

0

478

0

프로젝트 고민 해결이 안되어 다시 올립니다.

0

394

1

프로젝트 고민 다시올립니다.

0

452

1

친구와의 프로젝트 고민

0

424

1

화면 오른쪽 스크롤이 넘어가질 않아요!

0

517

1

학과 설정

0

334

2

회원가입폼 검증 -2 에서 문제가생겼습니다

0

332

1

card.html 버튼 위치

0

605

1

인스타그램 코드 작성 후 동작을 해보았는데 버튼을 눌러도 넘어가질않네요

0

360

2

carousel의 footer부분에

0

295

1

margin 값에 관한 질문입니다.

0

409

1

function clearMessages() 함수의 원리를 알고싶습니다

0

322

1

removeEventListener

0

240

1

버튼을 눌렀을때 케러셀의 이동

0

811

2