如何使用CSS和JavaScript建立一個可展開的網格?
可展開網格是指點選某個方塊時會展開的網格。預設情況下它是隱藏的,但點選方塊一次後會展開到100%。我們將建立三個相鄰的等寬列。點選任何一列,網格就會展開。此外,展開區域將有一個可關閉按鈕來關閉並返回到初始狀態。
建立一個包含三列的容器
從一個包含三列的父div開始。點選任何一個方塊,onclick屬性將開啟相應的選項卡:
<div class="container"> <div class="left" onclick="openTab('tab1');" ;> <h1>Some text on the left</h1> </div> <div class="center" onclick="openTab('tab2');"> <h1>Some text in center</h1> </div> <div class="right" onclick="openTab('tab3');"> <h1>Some text on the right</h1> </div> </div>
設定列的位置
使用float屬性將三列定位到左邊。所有這些列都並排浮動。寬度設定為33%,以便每列寬度相等:
.left, .right, .center { float: left; width: 33%; color: white; padding: 10px; height: 200px; text-align: center; }
開啟選項卡的指令碼
點選任何一個方塊,將使用下面的指令碼開啟選項卡:
<script> function openTab(tabName) { var i, x; x = document.querySelectorAll(".containerTab"); Array.from(x).forEach(item => { item.style.display = "none"; }); document.getElementById(tabName).style.display = "block"; } </script>
左側選項卡
以下是左側列的程式碼。點選“x”符號,它將關閉,因為onclick屬性設定為display: none。此部分預設隱藏:
<div id="tab1" class="containerTab" style="display:none;background:tomato"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 1</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
中間選項卡
以下是中間列的程式碼。點選“x”符號,它將關閉,因為onclick屬性設定為display: none。此部分預設隱藏:
<div id="tab2" class="containerTab" style="display:none;background:rgb(166, 71, 255)"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 2</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
右側選項卡
以下是右側列的程式碼。點選“x”符號,它將關閉,因為onclick屬性設定為display: none。此部分預設隱藏:
<div id="tab3" class="containerTab" style="display:none;background:teal"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 3</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
示例
要使用CSS和JavaScript建立一個可展開網格,程式碼如下:
<!DOCTYPE html> <html> <head> <style> body { padding: 1%; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } * { box-sizing: border-box; } .left, .right, .center { float: left; width: 33%; color: white; padding: 10px; height: 200px; text-align: center; } .left { background-color: tomato; } .right { background-color: teal; } .center { background-color: rgb(166, 71, 255); } .container:after { clear: both; } .closebtn { float: right; color: white; font-size: 35px; cursor: pointer; } .closebtn:hover { transform: scale(1.5); } .containerTab { padding: 20px; width: 99%; color: white; } </style> </head> <body> <h1 style="text-align: center;">Three Column grid example</h1> <div class="container"> <div class="left" onclick="openTab('tab1');" ;> <h1>Some text on the left</h1> </div> <div class="center" onclick="openTab('tab2');"> <h1>Some text in center</h1> </div> <div class="right" onclick="openTab('tab3');"> <h1>Some text on the right</h1> </div> </div> <div id="tab1" class="containerTab" style="display:none;background:tomato"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 1</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <div id="tab2" class="containerTab" style="display:none;background:rgb(166, 71, 255)"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 2</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <div id="tab3" class="containerTab" style="display:none;background:teal"> <span onclick="this.parentElement.style.display='none'" class="closebtn">×</span> <h2>Tab 3</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <script> function openTab(tabName) { var i, x; x = document.querySelectorAll(".containerTab"); Array.from(x).forEach(item => { item.style.display = "none"; }); document.getElementById(tabName).style.display = "block"; } </script> </body> </html>
廣告