วิธีการ - แท็บเต็มหน้า
เรียนรู้วิธีสร้างแท็บเต็มหน้า ซึ่งครอบคลุมทั้งหน้าต่างเบราว์เซอร์ด้วย 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) เพิ่ม JavaScript:
ตัวอย่าง
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();
เคล็ดลับ:ดูวิธีการ - แท็บด้วย