DIV CSS 佈局教程網

jQuery版Tab標簽切換
編輯:JQuery特效代碼     
鼠標移入Tab的時候會有一定的延時才會切換到相應的項,防止用戶不經意的鼠標操作,點擊相應的項也可以切換

效果圖:

源代碼:
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tab標簽切換</title>
</head>
<style>
body{ background:#fff;}
*{ margin:0; padding:0;}
.box{ margin:20px; }
.container{background:#FFF; width:500px; clear:both; margin-top:1px; _margin-top:-1px;}
.sub-con{height:300px; width:500px;border:1px solid #3CF; background:#FFF; display:none;}
.cur-sub-con{ display:block;}
.sub-con a{ line-height:40px}
.sub-con p{ text-align:center}
.nav{ width:370px; height:28px; margin-left:10px;}
.nav ul{width:370px; height:28px;}
.nav ul li{ list-style:none; display:inline-block;width:60px; height:28px;line-height:28px; text-align:center;margin-left:-9px;*float:left;*margin-left:-1px;}
.nav ul li a{ background:#fff;border:1px solid #3CF; text-decoration:none; color:#000; height:28px;display:block;}
.nav ul li a:hover{ background:#CCEDFB}
.nav ul li a.cur{ z-index:9999;border-bottom:1px solid #FFF; color:#F00;}
</style>
<body>
<div class="box">
<div class="nav">
<ul>
<li><a href="javascript:void(0)" class="cur">Tab項1</a></li>
<li><a href="javascript:void(0)">Tab項2</a></li>
<li><a href="javascript:void(0)">Tab項3</a></li>
<li><a href="javascript:void(0)">Tab項4</a></li>
</ul>
</div>
<div class="container">
<div class="sub-con cur-sub-con"> <a href="#" title="這是容器1">這是容器1</a><p><img src="http://images.cnblogs.com/cnblogs_com/cos2004/286060/o_img001.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器2">這是容器2</a><p><img src="http://images.cnblogs.com/cnblogs_com/cos2004/286060/o_img_g02.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器3">這是容器3</a><p><img src="http://images.cnblogs.com/cnblogs_com/cos2004/286060/o_img_g03.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器4">這是容器4</a><p><img src="http://images.cnblogs.com/cnblogs_com/cos2004/286060/o_img_g04.jpg" /></p></div>
</div>
</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var intervalID;
var curLi;
$(".nav li a").mouseover(function(){
curLi=$(this);
intervalID=setInterval(onMouseOver,250);//鼠標移入的時候有一定的延時才會切換到所在項,防止用戶不經意的操作
});
function onMouseOver(){
$(".cur-sub-con").removeClass("cur-sub-con");
$(".sub-con").eq($(".nav li a").index(curLi)).addClass("cur-sub-con");
$(".cur").removeClass("cur");
curLi.addClass("cur");
}
$(".nav li a").mouseout(function(){
clearInterval(intervalID);
});

$(".nav li a").click(function(){//鼠標點擊也可以切換
clearInterval(intervalID);
$(".cur-sub-con").removeClass("cur-sub-con");
$(".sub-con").eq($(".nav li a").index(curLi)).addClass("cur-sub-con");
$(".cur").removeClass("cur");
curLi.addClass("cur");
});

});
</script>
</html>


XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved