tab 펼쳤다 접었다하는 스크립트 소스 모음

thum

메뉴나 토글형식의 ui에서 자주쓰는 스크립트





//기본 누르면 열렸다 펼쳤다 하도록 설정

// Optional: Automatically close other tabs when one is opened
document.querySelectorAll('.tab-toggle').forEach(toggle => {
  toggle.addEventListener('change', function () {
    if (this.checked) {
      document.querySelectorAll('.tab-toggle').forEach(otherToggle => {
        if (otherToggle !== this) otherToggle.checked = false;
      });
    }
  });
});



// 첫 번째 탭은 항상 열려 있고, 다른 탭은 하나만 열리도록 설정
document.addEventListener('DOMContentLoaded', () => {
  const tabToggles = document.querySelectorAll('.tab-toggle');
  
  // 첫 번째 탭을 항상 체크된 상태로 설정하고 비활성화
  if (tabToggles.length > 0) {
    tabToggles[0].checked = true;
    tabToggles[0].disabled = true;
  }

  tabToggles.forEach((toggle, index) => {
    if (index === 0) return; // 첫 번째 탭은 이벤트 리스너를 추가하지 않음

    toggle.addEventListener('change', function () {
      if (this.checked) {
        tabToggles.forEach((otherToggle, otherIndex) => {
          if (otherIndex !== 0 && otherToggle !== this) {
            otherToggle.checked = false;
          }
        });
      }
    });
  });
});




// 처음 시작할 때 첫 번째 탭을 열고, 이후 하나의 탭만 열리도록 설정
document.addEventListener('DOMContentLoaded', () => {
  const tabToggles = document.querySelectorAll('.tab-toggle');
  
  // 처음 시작할 때 첫 번째 탭을 체크된 상태로 설정
  if (tabToggles.length > 0) {
    tabToggles[0].checked = true;
  }

  tabToggles.forEach(toggle => {
    toggle.addEventListener('change', function () {
      if (this.checked) {
        tabToggles.forEach(otherToggle => {
          if (otherToggle !== this) {
            otherToggle.checked = false;
          }
        });
      } else {
        // 모든 탭이 닫히는 것을 방지
        if ([...tabToggles].every(t => !t.checked)) {
          this.checked = true;
        }
      }
    });
  });
});



==================================


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Collapsible Tabs</title>
<script>
// 처음 시작할 때 첫 번째 탭을 열고, 이후 하나의 탭만 열리도록 설정
document.addEventListener('DOMContentLoaded', () => {
  const tabToggles = document.querySelectorAll('.tab-toggle');
  
  // 처음 시작할 때 첫 번째 탭을 체크된 상태로 설정
  if (tabToggles.length > 0) {
    tabToggles[0].checked = true;
  }

  tabToggles.forEach(toggle => {
    toggle.addEventListener('change', function () {
      if (this.checked) {
        tabToggles.forEach(otherToggle => {
          if (otherToggle !== this) {
            otherToggle.checked = false;
          }
        });
      } else {
        // 모든 탭이 닫히는 것을 방지
        if ([...tabToggles].every(t => !t.checked)) {
          this.checked = true;
        }
      }
    });
  });
});



</script>  
<style type="text/css">  
body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 20px;
  background-color: #f9f9f9;
}

.tabs {
  max-width: 600px;
  margin: 0 auto;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.tab {
  border-bottom: 1px solid #ddd;
}

.tab:last-child {
  border-bottom: none;
}

.tab-toggle {
  display: none;
}

.tab-label {
  display: block;
  padding: 15px 20px;
  background: #f1f1f1;
  color: #333;
  cursor: pointer;
  font-weight: bold;
  transition: background 0.3s ease;
}

.tab-label:hover {
  background: #e2e2e2;
}

.tab-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  padding: 0 20px;
  background: #fafafa;
}

.tab-content p {
  margin: 15px 0;
}

.tab-toggle:checked + .tab-label + .tab-content {
  max-height: 200px;
  padding: 15px 20px;
}
</style>
</head>
<body>
  <div class="tabs">
    <div class="tab">
      <input type="checkbox" id="tab-1" class="tab-toggle">
      <label for="tab-1" class="tab-label">Tab 1</label>
      <div class="tab-content">
        <p><span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           <span>Content for Tab 1. This is where you can place your details.< br/></span>
           Content for Tab 1. This is where you can place your details.< br/>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
            Content for Tab 1. This is where you can place your details.< /br>
        </p>
            
      </div>
    </div>

    <div class="tab">
      <input type="checkbox" id="tab-2" class="tab-toggle">
      <label for="tab-2" class="tab-label">Tab 2</label>
      <div class="tab-content">
        <p>Content for Tab 2. You can add more text or components here.</p>
      </div>
    </div>

    <div class="tab">
      <input type="checkbox" id="tab-3" class="tab-toggle">
      <label for="tab-3" class="tab-label">Tab 3</label>
      <div class="tab-content">
        <p>Content for Tab 3. Expand or collapse for more details.</p>
      </div>
    </div>
  </div>
  
</body>
</html>


==================
Collapsible Tabs

Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details. < br /> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br> Content for Tab 1. This is where you can place your details.< /br>

Content for Tab 2. You can add more text or components here.

Content for Tab 3. Expand or collapse for more details.





이 사이트의 모든 포스팅은 협찬 및 유료광고를 포함하고 있지 않습니다. 개인 취향에 따른 리뷰이니, 부디 참고만 하시길!


daily-inklings 전체 글 보기

이글은 에 최종으로 업데이트된 글입니다.

댓글

이 블로그의 인기 게시물

웹 성능 최적화를 위한 Image Lazy Loading 기법

블로그스팟 위젯 항상 펼치기 코딩 소스