วิธีการสร้าง: แท็บหน้าทั้งหมด
- หน้าก่อน ข้อหัวข้อแท็บ
- หน้าต่อไป แท็บเลื่อนหมุน
เรียนรู้ว่าจะสร้างแท็บหน้าทั้งหมดที่คลุมทั้งหมดหน้าตัวเเครื่อง (แท็บหน้าจอทั้งหมด)
แท็บหน้าทั้งหมด
คลิกลิงก์เพื่อแสดงหน้า "current"
บ้าน
บ้านคือที่หัวใจอยู่...
ข่าว
ข่าวนี้วันดีเลย!
ติดต่อ
ติดต่อเรา หรือมาเยี่ยมชมและรับกาแฟเดียวกัน
เกี่ยวกับ
ที่เราอยู่และอะไรที่เราทำ
สร้างแท็บหน้าเดี่ยว
ขั้นที่ 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>
创建按钮以打开特定的标签页内容。所有带有 class="tabcontent"
的 <div>
元素默认都是隐藏的(通过 CSS 和 JS)。当用户点击按钮时,它会打开与这个按钮“匹配”的标签页内容。
第二步 - 添加 CSS:
设置链接和标签页内容(全页)的样式:
/* 设置 body 和 document 的高度为 100%,以启用“全页标签页” body, html { height: 100%; margin: 0; font-family: Arial; } /* 设置标签页链接的样式 */ .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; } /* กำหนดรูปแบบของแท็บเนื้อหา (และเพิ่มความสูงเพื่อทั้งหน้า: 100%) */ .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;}
ขั้นที่สาม - แบบไฮเปอร์เท็กซ์จาวาสคริปต์:
function openPage(pageName, elmnt, color) { // ซ่อนทุกองค์ประกอบที่มี class="tabcontent" var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // ลบสีฝั่งหลังของทุกแบบแฟรม/ปุ่ม tablinks = document.getElementsByClassName("tablink"); for (i = 0; i < tablinks.length; i++) { tablinks[i].style.backgroundColor = ""; } // แสดงสิ่งที่เกี่ยวข้องกับแท็บเฉพาะ document.getElementById(pageName).style.display = "block"; // กำหนดสีเป็นเอาเท่านั้นสำหรับปุ่มที่เปิดหน้าแบบแฟรม elmnt.style.backgroundColor = color; } // ลบข้อมูล id="defaultOpen" และคลิกเข้าไป document.getElementById("defaultOpen").click();
หน้าที่เกี่ยวข้อง
คู่มือ:แบบจำลองของการสร้างแท็บ
- หน้าก่อน ข้อหัวข้อแท็บ
- หน้าต่อไป แท็บเลื่อนหมุน