【Sample】Tab layout for smartphone in jquery.cookie.js size variableness

Demo

 Enter to Demo


HTML

 Read jQuery 1.x snippet in <head> from Google Hosted Libraries.


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

 Use jquery.cookie.js, Download jquery.cookie.js from GitHub and insert it in the same way in <head>.


<script src="jquery.cookie.js"></script>

<div id="tab">
<ul id="tab-head">
<li id="tab-h1"><a href="#tab-b1">menu1</a></li>
<li id="tab-h2"><a href="#tab-b2">menu2</a></li>
<li id="tab-h3"><a href="#tab-b3">menu3</a></li>
<li id="tab-h4"><a href="#tab-b4">menu4</a></li>
<!--tab-head--></ul>
<div id="tab-body">
<div id="tab-b1">content1</div>
<div id="tab-b2">content2</div>
<div id="tab-b3">content3</div>
<div id="tab-b4">content4</div>
<!--tab-body--></div>
<!--tab--></div>
CSS

.sample-demo #tab {
	margin: 0 10px;
}
.sample-demo #tab-head {
	width: 400px;
	overflow: hidden;
}
.sample-demo #tab-head li {
	float: left;
}
.sample-demo #tab-head li a {
	display: block;
	width: 98px;
	height: 25px;
	text-align: center;
}
.sample-demo #tab-body {
	width: 100%;/*The width sets it here. It becomes variable if You will have 100%.*/
	min-width: 400px;
	position: relative;
	top: -1px;
	z-index: -1;
}
.sample-demo #tab-b1,#tab-b2,#tab-b3,#tab-b4 {
	height: 300px;
	padding: 10px;
	background: #fff;
	border: 1px solid #ccc;
}
.sample-demo #tab-b2,
.sample-demo #tab-b3,
.sample-demo #tab-b4 {
	display: none;
}
.sample-demo .tab1 {
	background: #eee;
	border-top: 1px solid #ccc;
	border-right: 1px solid #ccc;
	border-left: 1px solid #ccc;
}
.sample-demo .tab2 {
	background: #fff;
	border-bottom: 1px solid #fff;
}
JavaScript

$(document).ready(function(){
$("#tab-head li").addClass("tab1");
for(var i=0; i < $("#tab-body div").length; i++) {
var name = "cookie-tab";
if($.cookie(name)) {
if(($.cookie(name).indexOf(i)) != -1) {
$("#tab-body div").eq(i).show();
$("#tab-head li a").eq(i).addClass("tab2");
} else {
$("#tab-body div").eq(i).hide();
$("#tab-head li a").eq(i).removeClass("tab2");
}
}else{
$("#tab-body div").eq(0).show();
$("#tab-head li a").eq(0).addClass("tab2");
}
};
$("#tab-head li a").click(function(){
$("#tab-body div").hide();
$($(this).attr("href")).fadeIn();/*Control it with an animation*/
//$($(this).attr("href")).css('display','block');/*Control it by CSS*/
$("#tab-head li a").removeClass("tab2");
$(this).addClass("tab2");
var index = $("#tab-head li a").index(this);
$.cookie(name,index,{expires:7});
return false;
})
});
Back to the previous page