Publish/JavaScript

div 내부에 있는 스크롤 영역 계산, 스크롤시 섹션에 맞는 tab에 active ( position() )

아2 2022. 10. 1. 10:46
  // 스크롤시 섹션에 맞는 tab에 active 주기
  $("#wrap_content").scroll(function () {
    const height = $("#wrap_content").scrollTop(); // 해당 스크롤 좌표값(높이)

    const content = $('.content_area');

    content.each(function () {
      const a = $(this).position();

      if (a.top == height || a.top < height + 200) {

        const thisIndex = $(this).index()
        const tabIndex = $('.tab').eq(thisIndex);

        tabIndex.addClass('active'); // 
        $('.tab').not(tabIndex).removeClass('active')
      }
    })
  });