w3.css
전체 페이지 탭
CSS 및 JavaScript를 사용하여 전체 브라우저 창을 덮는 전체 페이지 탭을 만드는 방법을 알아보자.
한 페이지 탭 만들기
1단계) HTML 추가
<button class="tablink" onclick="openPage('Home', this, 'red')">Home</button> <button class="tablink" onclick="openPage('News', this, 'green')" id="defaultOpen">News</button> <button class="tablink" onclick="openPage('Contact', this, 'blue')">Contact</button> <button class="tablink" onclick="openPage('About', this, 'orange')">About</button> <div id="Home" class="tabcontent"> <h3>Home</h3> <p>Home is where the heart is..</p> </div> <div id="News" class="tabcontent"> <h3>News</h3> <p>Some news this fine day!</p> </div> <div id="Contact" class="tabcontent"> <h3>Contact</h3> <p>Get in touch, or swing by for a cup of coffee.</p> </div> <div id="About" class="tabcontent"> <h3>About</h3> <p>Who we are and what we do.</p> </div>
특정 탭 콘텐츠를 여는 버튼을 만든다.
모든 <div> 요소는 class=”tabcontent”기본적으로 숨겨져 있다(CSS 및 JS 포함).
사용자가 버튼을 클릭하면 이 버튼과 “일치하는” 탭 콘텐츠가 열린다.
2단계) CSS 추가
링크와 탭 내용의 스타일을 지정한다(전체 페이지).
/* Set height of body and the document to 100% to enable "full page tabs" */ body, html { height: 100%; margin: 0; font-family: Arial; } /* Style tab links */ .tablink { background-color: #555; color: white; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; font-size: 17px; width: 25%; } .tablink:hover { background-color: #777; } /* Style the tab content (and add height:100% for full page content) */ .tabcontent { color: white; display: none; padding: 100px 20px; height: 100%; } #Home {background-color: red;} #News {background-color: green;} #Contact {background-color: blue;} #About {background-color: orange;}
3단계) 자바스크립트 추가
function openPage(pageName, elmnt, color) { // Hide all elements with class="tabcontent" by default */ var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // Remove the background color of all tablinks/buttons tablinks = document.getElementsByClassName("tablink"); for (i = 0; i < tablinks.length; i++) { tablinks[i].style.backgroundColor = ""; } // Show the specific tab content document.getElementById(pageName).style.display = "block"; // Add the specific color to the button used to open the tab content elmnt.style.backgroundColor = color; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click();
기본 예시
예제 보기참고
W3C School - How TO - Full Page Tabs