当前位置: 编程技术>jquery
Jquery 经典选项卡的实现代码一例
来源: 互联网 发布时间:2014-09-03
本文导语: 需要引入外部Jquery文件与Css文件,大家自行完成吧。 代码如下: 选项卡-www. /* 选项卡 */ .tab{width:500px;border:#ccc 1px solid;margin:100px;} .tab dl dt{ border-bottom:#ccc 1px solid;height:25px;background:#f1f1f1; margin-bottom:-1px;} .tab dl dt a{f...
需要引入外部Jquery文件与Css文件,大家自行完成吧。
代码如下:
选项卡-www.
/* 选项卡 */
.tab{width:500px;border:#ccc 1px solid;margin:100px;}
.tab dl dt{ border-bottom:#ccc 1px solid;height:25px;background:#f1f1f1; margin-bottom:-1px;}
.tab dl dt a{float:left;display:block;cursor:pointer;width:60px;height:25px;line-height:25px;
text-align:center;background:#f1f1f1;color:#000;}
.tab dl dt a.tabActive{background:#fff;color:#333;font-weight:bold;border-bottom:1px solid #fff;
position:relative;border-right:1px solid #ccc;border-left:1px solid #ccc;}
.tab dl dd{padding:10px;height:200px; clear:both;}
// 选项卡
$(function(){
$(".tab dl dt>a:first").addClass("tabActive");
$(".tab dl dd ul").not(":first").hide();
$(".tab dl dt>a").unbind("click").bind("click", function(){
$(this).siblings("a").removeClass("tabActive").end().addClass("tabActive");
var index = $(".tab dl dt>a").index( $(this) );
$(".tab dl dd ul").eq(index).siblings(".tab dl dd ul").hide().end().fadeIn("slow");
});
});
// 自动轮换选项卡
$(document).ready(function(){
$('.tab dl dt a:first').addClass('tabActive');
$('.tab dl dd ul:first').css('display','block');
autoroll();
hookThumb();
});
var i=-1; //第i+1个tab开始
var offset = 2500; //轮换时间
var timer = null;
function autoroll(){
n = $('.tab dl dt a').length-1;
i++;
if(i > n){
i = 0;
}
slide(i);
timer = window.setTimeout(autoroll, offset);
}
function slide(i){
$('.tab dl dt a').eq(i).addClass('tabActive').siblings().removeClass('tabActive');
$('.tab dl dd ul').eq(i).fadeIn("slow").siblings('.tab dl dd ul').hide();
}
function hookThumb(){
$('.tab dl dt a').hover(
function () {
if (timer) {
clearTimeout(timer);
i = $(this).prevAll().length;
slide(i);
}
},
function () {
timer = window.setTimeout(autoroll, offset);
this.blur();
return false;
}
);
}