วิธีการ - แท็บ
เรียนรู้วิธีสร้างแท็บด้วย CSS และ JavaScript
แท็บ
แท็บเหมาะอย่างยิ่งสำหรับเว็บแอปพลิเคชันหน้าเดียว หรือสำหรับหน้าเว็บที่สามารถแสดงหัวข้อต่างๆ ได้:
ลอนดอน
ลอนดอนเป็นเมืองหลวงของอังกฤษ
สร้างแท็บสลับได้
ขั้นตอนที่ 1) เพิ่ม HTML:
ตัวอย่าง
<!-- Tab links -->
<div class="tab">
<button class="tablinks" onclick="openCity(event,
'London')">London</button>
<button class="tablinks" onclick="openCity(event,
'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event,
'Tokyo')">Tokyo</button>
</div>
<!-- Tab content -->
<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> class="tabcontent"
ทั้งหมดที่ถูกซ่อนไว้โดยค่าเริ่มต้น (ด้วย CSS & JS) เมื่อผู้ใช้คลิกที่ปุ่ม - จะเป็นการเปิดเนื้อหาแท็บที่ "ตรงกัน" ปุ่มนี้
ขั้นตอนที่ 2) เพิ่ม CSS:
จัดรูปแบบปุ่มและเนื้อหาแท็บ:
ตัวอย่าง
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons that are used to open the tab content */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
/* Change background color of buttons on hover
*/
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active
{
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
ขั้นตอนที่ 3) เพิ่ม JavaScript:
ตัวอย่าง
function openCity(evt, cityName) {
// Declare all
variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent
= document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove
the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i <
tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to
the button that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
จางลงในแท็บ:
หากคุณต้องการจางเนื้อหาในแท็บ ให้เพิ่ม CSS ต่อไปนี้:
ตัวอย่าง
.tabcontent {
animation: fadeEffect 1s; /* Fading effect takes 1 second */
}
/* Go
from zero to full opacity */
@keyframes fadeEffect {
from
{opacity: 0;}
to {opacity: 1;}
}
แสดงแท็บโดยค่าเริ่มต้น
ในการเปิดแท็บเฉพาะในการโหลดหน้าเว็บ ให้ใช้ JavaScript เพื่อ "คลิก" บนปุ่มแท็บที่ระบุ:
ตัวอย่าง
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
<script>
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
ปิดแท็บ
หากคุณต้องการปิดแท็บใดแท็บหนึ่ง ให้ใช้ JavaScript เพื่อซ่อนแท็บด้วยการคลิกปุ่ม:
ตัวอย่าง
<!-- Click on the <span> element to close the tab -->
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
<span onclick="this.parentElement.style.display='none'">x</span>
</div>
เคล็ดลับ:ดูวิธีการ - แท็บแนวตั้งด้วย