tab 클릭 시 css 변경
2022. 10. 24. 12:27ㆍjavaScript
탭 메뉴나 페이지 번호 등을 클릭했을 때 클릭한 요소만 css를 변경(.active 추가)하는 js
HTML
<div id="container">
<span class="tab active">1</span>
<span class="tab">2</span>
<span class="tab">3</span>
<span class="tab">4</span>
<span class="tab">5</span>
</div>
css
#container > .tab {color:#333;}
#container > .tab.active {color:#fff; background:#333; border-radius:50%;}
JS
const tabContainer = document.getElementById("container");
let tab = tabContainer.getElementsByClassName("tab");
for(let i = 0; i < tab.length; i++){
let current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
}
참고사이트
'javaScript' 카테고리의 다른 글
locomotive-scroll)스크롤 부드럽게 하기 (0) | 2022.11.08 |
---|---|
smooth scrollbar.js)스크롤 부드럽게 하기 (0) | 2022.11.08 |
JS로 별모양 찍기 (for문) (0) | 2022.08.25 |
div로 progress bar 만들기 (scroll indicator) (0) | 2022.08.02 |
scrollTo를 이용한 맨 위로 / 맨 아래로 버튼 만들기 (0) | 2022.07.22 |