Membuat Togglabel Tab Headers
Langkah pertama:
Add HTML berikut
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="Oslo" class="tabcontent">
<h3>Oslo</h3>
<p>Oslo is the capital of Norway.</p>
</div>
<button class="tablink" onclick="openCity('London', this, 'red')"id="defaultOpen">London</button>
<button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button>
<button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
<button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button>
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
<div id="Oslo" class="tabcontent">
<h3>Oslo</h3>
<p>Oslo is the capital of Norway.</p>
</div>
<button class="tablink" onclick="openCity('London', this, 'red')"id="defaultOpen">London</button>
<button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button>
<button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
<button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button>
Buat tombol untuk membuka konten tertentu. Semua elemen <div> dengan class="tabcontent" disembunyikan secara default dengan CSS dan juga JS. Saat pengguna mengklik tombol , ia akan mencocokkan konte tab yang kita klik.
Langkah kedua:
Add CSS berikut untuk style tombol dan konten tab
/* Style the tab buttons */
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
width: 25%;}
/* Change background color of buttons on hover */
.tablink:hover {
background-color: #777;}
/* Set default styles for tab content */
.tabcontent {
color: white;
display: none;
padding: 50px;
text-align: center;}
/* Style each tab content individually */
#London {background-color:red;}
#Paris {background-color:green;}
#Tokyo {background-color:blue;}
#Oslo {background-color:orange;}
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
width: 25%;}
/* Change background color of buttons on hover */
.tablink:hover {
background-color: #777;}
/* Set default styles for tab content */
.tabcontent {
color: white;
display: none;
padding: 50px;
text-align: center;}
/* Style each tab content individually */
#London {background-color:red;}
#Paris {background-color:green;}
#Tokyo {background-color:blue;}
#Oslo {background-color:orange;}
Langkah ketiga:
Add juga Javascript berikut
function openCity(cityName, 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(cityName).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 itdocument.getElementById("defaultOpen").click();
// 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(cityName).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 itdocument.getElementById("defaultOpen").click();
Itu tadi cara membuat sebuah Tab Headers dengan HTML dan CSS dan juga JS, semoga bermanfaat bagi pembaca, sekian terima kasih dan sampai jumpa...
No comments:
Post a Comment