I am trying to make my css tabs compatible for mobile devices, especially for IOS. Also I want to use 3 different tabs on my page. I have no jquery knowledge, so I tried to find an example. The best example I found was using this structure;
<ul id="tabs">
<li><a href="#" name="tab1">One</a></li>
<li><a href="#" name="tab2">Two</a></li>
<li><a href="#" name="tab3">Three</a></li>
<li><a href="#" name="tab4">Four</a></li>
</ul>
<div id="content">
<div id="tab1">...</div>
<div id="tab2">...</div>
<div id="tab3">...</div>
<div id="tab4">...</div>
</div>
and its jQuery is;
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$("#content div").hide(); // Initially hide all content
$("#tabs li:first").attr("id","current"); // Activate first tab
$("#content div:first").fadeIn(); // Show first tab content
$('#tabs a').click(function(e) {
e.preventDefault();
if ($(this).closest("li").attr("id") == "current"){ //detection for current tab
return
}
else{
$("#content div").hide(); //Hide all content
$("#tabs li").attr("id",""); //Reset id's
$(this).parent().attr("id","current"); // Activate this
$('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
}
});
});
</script>
There is a small problem. I do not want to use id for tabs. I changed id with classes then modified the CSS. I tried to modify the script but it wont work:(
There are three different tab groups on my page, that’s why I am changing it to classes.
My new tabs are;
<ul class="tabs">
<li><a href="#" name="tab1">One</a></li>
<li><a href="#" name="tab2">Two</a></li>
<li><a href="#" name="tab3">Three</a></li>
<li><a href="#" name="tab4">Four</a></li>
</ul>
<div class="tabs_content">
<div id="tab1">...</div>
<div id="tab2">...</div>
<div id="tab3">...</div>
<div id="tab4">...</div>
</div>
And tried to change jQuery as;
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$(".tabs_content div").hide(); // Initially hide all content
$(".tabs li:first").attr("id","current"); // Activate first tab
$(".tabs_content div:first").fadeIn(); // Show first tab content
$('.tabs a').click(function(e) {
e.preventDefault();
if ($(this).closest("li").attr("id") == "current"){ //detection for current tab
return
}
else{
$(".tabs_content div").hide(); //Hide all content
$(".tabs li").attr("id",""); //Reset id's
$(this).parent().attr("id","current"); // Activate this
$('#' + $(this).attr('name')).fadeIn(); // Show content for current tab
}
});
});
</script>
Can you please help me with the jQuery? Or show me a better example for multiple tab tables within the same page?
Thanx in advance
edit: With the change now when I use one tab group other tab groups’ contents are hidden 🙂
And this is the CSS file;
.tabs{
overflow: hidden;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
}
.tabs li{
float: left;
margin: 0 .5em 0 0;
}
.tabs a{
position: relative;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
padding: .7em 3.5em;
float: left;
text-decoration: none;
color: #444;
text-shadow: 0 1px 0 rgba(255,255,255,.8);
border-radius: 5px 0 0 0;
box-shadow: 0 2px 2px rgba(0,0,0,.4);
}
.tabs a:hover,
.tabs a:hover::after,
.tabs a:focus,
.tabs a:focus::after{
background: #fff;
}
.tabs a:focus{
outline: 0;
}
.tabs a::after{
content:'';
position:absolute;
z-index: 1;
top: 0;
right: -.5em;
bottom: 0;
width: 1em;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
box-shadow: 2px 2px 2px rgba(0,0,0,.4);
transform: skew(10deg);
border-radius: 0 5px 0 0;
}
.tabs #current a,
.tabs #current a::after{
background: #fff;
z-index: 3;
}
.tabs_content
{
background: #fff;
padding: 2em;
height: 220px;
position: relative;
z-index: 2;
border-radius: 0 5px 5px 5px;
box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);
}
My new settings are;
Script is;
<script type="text/javascript" src="jquery-1.8.1.js"></script>
<script>
$(function(){
//INIT IS NOT NECESSARY IF YOU FOLLOW MY CODE
$("#tabs a").click(function(){
//ADD ACTIVE CLASS
$(this).parent().siblings().removeClass("current");
$(this).parent().addClass("current");
//SHOW CONTENT
$(".content-holder").removeClass("current");
var myIndex = $(this).parent().index(); // gets the index of the LI clicked
$(".content-holder").eq(myIndex).fadeIn();
});
});
</script>
HTML is;
<ul class="tabs">
<li class="tab current"><a href="#" name="tab1">One</a></li>
<li class="tab"><a href="#" name="tab2">Two</a></li>
<li class="tab"><a href="#" name="tab3">Three</a></li>
<li class="tab"><a href="#" name="tab4">Four</a></li>
</ul>
<div class="content">
<div class="content-holder current" id="tab1">content1</div>
<div class="content-holder" id="tab2">content2</div>
<div class="content-holder" id="tab3">content3</div>
<div class="content-holder" id="tab4">content4</div>
</div>
and my css is;
.tabs{
overflow: hidden;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
}
.tabs li{
float: left;
margin: 0 .5em 0 0;
}
.tabs a{
position: relative;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
padding: .7em 3.5em;
float: left;
text-decoration: none;
color: #444;
text-shadow: 0 1px 0 rgba(255,255,255,.8);
border-radius: 5px 0 0 0;
box-shadow: 0 2px 2px rgba(0,0,0,.4);
}
.tabs a:hover,
.tabs a:hover::after,
.tabs a:focus,
.tabs a:focus::after{
background: #fff;
}
.tabs a:focus{
outline: 0;
}
.tabs a::after{
content:'';
position:absolute;
z-index: 1;
top: 0;
right: -.5em;
bottom: 0;
width: 1em;
background: #ddd;
background-image: linear-gradient(to bottom, #fff, #ddd);
box-shadow: 2px 2px 2px rgba(0,0,0,.4);
transform: skew(10deg);
border-radius: 0 5px 0 0;
}
.tabs #current a,
.tabs #current a::after{
background: #fff;
z-index: 3;
}
.content
{
background: #fff;
padding: 2em;
height: 220px;
position: relative;
z-index: 2;
border-radius: 0 5px 5px 5px;
box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);
}
.content-holder {
display:none;
}
.content-holder.current {
display:block;
}
I setup a plugin that can handle multiple tab sets.
You’ll have to wrap your html in a div containing both the tabs and the content. Also you need to update your anchors to use a standard hash tag, see below:
You can call it like so:
You can see a working example including three tab sets at: http://jsfiddle.net/rustyjeans/XVp5X/