How to Create Custom Ajax Tabs with Jquery
HTML :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Tabs Demo</title>
<link rel="stylesheet" href="./main.css" />
<script type="text/javascript" src="./jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="./main.js"></script>
</head>
<body>
<div id="page">
<ul id="tabs">
<li><a href="./tabs/tab-1.html">Tab 1</a></li>
<li><a href="./tabs/tab-2.html">Tab 2</a></li>
<li><a href="./tabs/tab-3.html">Tab 3</a></li>
</ul>
<div id="tabs-container">
Loading. Please Wait...
</div>
</div>
</body>
</html>
CSS:
BODY {
margin: 20px;
padding: 20px;
font-family: "Lucida Grande", Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
#page {
margin: auto;
width: 1000px;
}
UL.mytabs {
position: relative;
z-index: 2;
}
UL.mytabs, UL.mytabs LI {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
UL.mytabs LI { padding: 0 5px; }
UL.mytabs LI A {
float: left;
padding: 7px;
border: 1px solid #CCCCCC;
border-bottom: 1px solid #E0E0E0;
background: #F0F0F0;
text-decoration: none;
color: #333333;
height: 22px;
}
UL.mytabs LI A:HOVER, UL.mytabs LI.current A {
background: #FFFFFF;
}
UL.mytabs LI.current A {
font-weight: bold;
font-size: 14px;
border-bottom: 1px solid #FFFFFF;
}
.mytabs-container {
position: relative;
z-index: 1;
clear: both;
border: 1px solid #E0E0E0;
padding: 10px;
top: -1px;
}
Javascript :
var containerId = '#tabs-container';
var tabsId = '#tabs';
$(document).ready(function(){
// Preload tab on page load
if($(tabsId + ' LI.current A').length > 0){
loadTab($(tabsId + ' LI.current A'));
}
$(tabsId + ' A').click(function(){
if($(this).parent().hasClass('current')){ return >false; }
$(tabsId + ' LI.current').removeClass('current');
$(this).parent().addClass('current');
loadTab($(this));
return >false;
});
});
function loadTab(tabObj){
if(!tabObj || !tabObj.length){ return; }
$(containerId).addClass('loading');
$(containerId).fadeOut('fast');
$(containerId).load(tabObj.attr('href'), function(){
$(containerId).removeClass('loading');
$(containerId).fadeIn('fast');
});
}
Tags: Ajax, Jquery, Ajax Tabs with Jquery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Tabs Demo</title>
<link rel="stylesheet" href="./main.css" />
<script type="text/javascript" src="./jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="./main.js"></script>
</head>
<body>
<div id="page">
<ul id="tabs">
<li><a href="./tabs/tab-1.html">Tab 1</a></li>
<li><a href="./tabs/tab-2.html">Tab 2</a></li>
<li><a href="./tabs/tab-3.html">Tab 3</a></li>
</ul>
<div id="tabs-container">
Loading. Please Wait...
</div>
</div>
</body>
</html>
CSS:
BODY {
margin: 20px;
padding: 20px;
font-family: "Lucida Grande", Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
#page {
margin: auto;
width: 1000px;
}
UL.mytabs {
position: relative;
z-index: 2;
}
UL.mytabs, UL.mytabs LI {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
UL.mytabs LI { padding: 0 5px; }
UL.mytabs LI A {
float: left;
padding: 7px;
border: 1px solid #CCCCCC;
border-bottom: 1px solid #E0E0E0;
background: #F0F0F0;
text-decoration: none;
color: #333333;
height: 22px;
}
UL.mytabs LI A:HOVER, UL.mytabs LI.current A {
background: #FFFFFF;
}
UL.mytabs LI.current A {
font-weight: bold;
font-size: 14px;
border-bottom: 1px solid #FFFFFF;
}
.mytabs-container {
position: relative;
z-index: 1;
clear: both;
border: 1px solid #E0E0E0;
padding: 10px;
top: -1px;
}
Javascript :
var containerId = '#tabs-container';
var tabsId = '#tabs';
$(document).ready(function(){
// Preload tab on page load
if($(tabsId + ' LI.current A').length > 0){
loadTab($(tabsId + ' LI.current A'));
}
$(tabsId + ' A').click(function(){
if($(this).parent().hasClass('current')){ return >false; }
$(tabsId + ' LI.current').removeClass('current');
$(this).parent().addClass('current');
loadTab($(this));
return >false;
});
});
function loadTab(tabObj){
if(!tabObj || !tabObj.length){ return; }
$(containerId).addClass('loading');
$(containerId).fadeOut('fast');
$(containerId).load(tabObj.attr('href'), function(){
$(containerId).removeClass('loading');
$(containerId).fadeIn('fast');
});
}
Tags: Ajax, Jquery, Ajax Tabs with Jquery
Comments
Post a Comment