JS实现的选项卡切换

<!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>test</title>
<style>
*{margin:0;padding:0;}
body{font-size:14px;font-family:"Microsoft YaHei";}
ul,li{list-style:none;}
#tab{position:relative;}
#tab .tabList ul li{
    float:left;
    background:#fefefe;
    background:-moz-linear-gradient(top, #fefefe, #ededed);  
    background:-o-linear-gradient(left top,left bottom, from(#fefefe), to(#ededed));
    background:-webkit-gradient(linear,left top,left bottom, from(#fefefe), to(#ededed));
    border:1px solid #ccc;
    padding:5px 0;
    width:100px;
    text-align:center;
    margin-left:-1px;
    position:relative;
    cursor:pointer;
}
#tab .tabCon{
    position:absolute;
    left:-1px;
    top:32px;
    border:1px solid #ccc;
    border-top:none;
    width:403px;
    height:100px;
}
#tab .tabCon div{
    padding:10px;
    position:absolute;
    opacity:0;
    filter:alpha(opacity=0);
}
#tab .tabList li.cur{
    border-bottom:none;
    background:#fff;
}
#tab .tabCon div.cur{
    opacity:1;
    filter:alpha(opacity=100);
}
</style>
</head>
<body>
<div id="tab" style="margin-left:460px;margin-top:20px">
  <div class="tabList">
    <ul>
        <li class="cur">菜单1</li>
        <li>菜单2</li>
        <li>菜单3</li>
        <li>菜单4</li>
    </ul>
  </div>
  <div class="tabCon">
    <div class="cur">内容11111111111111111111111111</div>
    <div>内容222222222222222222222222222</div>
    <div>内容33333333333333333333333</div>
    <div>内容4444444444444444444444444444</div>
  </div>
</div>
<script>
window.onload = function() {
    var oDiv = document.getElementById("tab");
    var oLi = oDiv.getElementsByTagName("div")[0].getElementsByTagName("li");
    var aCon = oDiv.getElementsByTagName("div")[1].getElementsByTagName("div");
    var timer = null;
    for (var i = 0; i < oLi.length; i++) {
        oLi[i].index = i;
        oLi[i].onmouseover = function() {
            show(this.index);
        }
    }
    function show(a) {
        index = a;
        var alpha = 0;
        for (var j = 0; j < oLi.length; j++) {
            oLi[j].className = "";
            aCon[j].className = "";
            aCon[j].style.opacity = 0;
            aCon[j].style.filter = "alpha(opacity=0)";
        }
        oLi[index].className = "cur";
        clearInterval(timer);
        timer = setInterval(function() {
            alpha += 2;
            alpha > 100 && (alpha = 100);
            aCon[index].style.opacity = alpha / 100;
            aCon[index].style.filter = "alpha(opacity=" + alpha + ")";
            alpha == 100 && clearInterval(timer);
        },
        5)
    }
}
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值