滑鼠移入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://www.bkjia.com/uploads/allimg/131016/1649442930-1.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器2">這是容器2</a><p><img src="http://www.bkjia.com/uploads/allimg/131016/1649443041-2.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器3">這是容器3</a><p><img src="http://www.bkjia.com/uploads/allimg/131016/1649441U9-3.jpg" /></p></div>
<div class="sub-con"> <a href="#" title="這是容器4">這是容器4</a><p><img src="http://www.bkjia.com/uploads/allimg/131016/1649443514-4.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>