了解如何創建CSS和JavaScript選項卡。
標籤
標籤是完美的單頁Web應用程序,或者能夠顯示不同科目的網頁:
倫敦
倫敦是英國的首都城市。
巴黎
巴黎是法國的首都。
東京
東京是日本的首都。
創建Togglable標籤
步驟1)添加HTML:
例
<ul class="tab">
<li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li>
<li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li>
<li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li>
</ul>
<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"
默認隱藏(with CSS & JS) -當用戶點擊一個鏈接-這將打開的選項卡內容"matches"此鏈接。
步驟2)添加CSS:
樣式列表和標籤內容:
例
/* Style the list */
ul.tab {
list-style-type:
none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Float the list items side by side */
ul.tab li {float: left;}
/* Style the links inside the list items */
ul.tab li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.tab li a:hover
{background-color: #ddd;}
/* Create an active/current tablink class
*/
ul.tab li a:focus, .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 <
tabcontent.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to
the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
試一試» 淡入標籤:
如果你想在標籤內容褪色,添加以下的CSS:
例
.tabcontent {
-webkit-animation: fadeEffect 1s;
animation: fadeEffect 1s; /* Fading effect takes 1 second */
}
@-webkit-keyframes fadeEffect {
from
{opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeEffect {
from
{opacity: 0;}
to {opacity: 1;}
}
試一試»