होवर पर टैब स्विचिंग
CSS और JavaScript से होवर पर टैब्स बदलने का कोड:
उदाहरण
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* { box-sizing: border-box }
.tab {
float: left;
border: 1px solid #ccc;
background-color: #f1f1f1;
width: 30%;
height: 300px;
}
.tab button {
display: block;
background-color: inherit;
color: black;
padding: 22px 16px;
width: 100%;
border: none;
outline: none;
text-align: left;
cursor: pointer;
font-size: 17px;
}
.tab button:hover {
background-color: #ddd;
}
.tab button.active {
background-color: #ccc;
}
.tabcontent {
float: left;
padding: 0px 12px;
border: 1px solid #ccc;
width: 70%;
border-left: none;
height: 300px;
display: none;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
</style>
</head>
<body>
<h2>डेमो हेडिंग टैब्स</h2>
<p>विभाग की जानकारी के लिए टैब्स पर होवर करें:</p>
<div class="tab">
<button class="tablinks" onmouseover="demo(event, 'Marketing')">Marketing</button>
<button class="tablinks" onmouseover="demo(event, 'Operations')">Operations</button>
<button class="tablinks" onmouseover="demo(event, 'Finance')">Finance</button>
<button class="tablinks" onmouseover="demo(event, 'IT')">IT</button>
</div>
<div id="Marketing" class="tabcontent"><h3>Marketing</h3></div>
<div id="Operations" class="tabcontent"><h3>Operations</h3></div>
<div id="Finance" class="tabcontent"><h3>Finance</h3></div>
<div id="IT" class="tabcontent"><h3>IT</h3></div>
<div class="clearfix"></div>
<script>
function demo(e, dept) {
var i, content, links;
content = document.getElementsByClassName("tabcontent");
for (i = 0; i < content.length; i++) {
content[i].style.display = "none";
}
links = document.getElementsByClassName("tablinks");
for (i = 0; i < links.length; i++) {
links[i].className = links[i].className.replace(" active", "");
}
document.getElementById(dept).style.display = "block";
e.currentTarget.className += " active";
}
</script>
</body>
</html>
आउटपुट
- प्रारंभ में: कोई टैब कंटेंट नहीं दिखता (सभी
display: none) - किसी टैब बटन पर होवर करने पर: संबंधित कंटेंट दिखता है, बटन को
activeक्लास मिलती है - होवर हटाने पर: कंटेंट छुपा रहता है (क्लिक-आधारित के विपरीत)
महत्वपूर्ण विचार
- होवर vs क्लिक: होवर मोबाइल/टच डिवाइस पर काम नहीं करता। क्लिक/टैप बेहतर UX है।
- एक्सेसिबिलिटी: कीबोर्ड नेविगेशन (Tab, Enter/Space) के लिए
onclickयाonkeydownजोड़ें। - ARIA:
role="tablist",role="tab",role="tabpanel",aria-selectedजोड़ें।
केवल CSS विकल्प (नो JS)
.tabcontent { display: none; }
.tab button:hover + .tabcontent,
.tab button:focus + .tabcontent { display: block; }
/* या सिबलिंग सेलेक्टर के साथ */