仿126邮箱tab切换(展开+关闭)原生js实现切换效果

仿照126

目前大多基于bootstrap管理后台如ACE等,都已经集成常见需求。
但是类似126邮箱这种没有找到。
在此贴上原生js代码实现,有用之处copy即可。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>仿照126邮箱tab效果</title>
<style>
*{ margin:0; padding:0; list-style:none;}
#box{ width:2000px; background:#ccc; margin:0 auto; overflow:hidden;}
#l{ width:200px; float:left; }
#l li{ width:198px; height:30px; font-size:24px; line-height:24px; text-align:center; font-weight:bold; margin-bottom:10px; border:1px solid blue; background:#9CF; cursor:pointer;}
#l .on{ background:#f90;}

#r{ width:1800px; float:right}
#rt{ width:1800px; height:50px; background:yellow; overflow:hidden;} 
#rt li{ width:180px; height:48px; line-height:48px; text-align:center; float:left; background:#fff; border:1px solid #000; margin-right:2px; position:relative; top:0; left:0;}
#rt li.active{ background:#f09;}
#rt li span{ position:absolute; top:0; right:0; height:20px; width:20px; background:#CCC; font-size:18px; text-align:center; line-height:20px; cursor:pointer;}
#rb{ width:1800px;}
#rb div{ width:500px; height:330px; text-align:center; line-height:330px; font-size:60px; font-weight:bold; background:#6F0;}

</style>
<script> 
window.onload=function(){
    var oUl=document.getElementById('l');
    var oRT=document.getElementById('rt');
    var oRB=document.getElementById('rb');
    var aLi=oUl.children;
    var arr=[];
    var bOk=false;
    var bOk2=false;
    for(var i=0;i<aLi.length;i++){
        (function(index){
            aLi[index].onclick=function(){
                for(var i=0;i<aLi.length;i++){
                    aLi[i].className='';    
                }    
                this.className='on';
                var aRTli=oRT.children;
                var aRBdiv=oRB.children;
                if(aRTli.length){//有length
                    for(var i=0;i<arr.length;i++){
                        if(arr[i].b==index){
                            bOk=true;
                        }
                    }
                    if(bOk){//如果遍历找到
                        for(var i=0;i<aRTli.length;i++){
                            aRTli[i].className='';
                            aRBdiv[i].style.display='none';    
                            
                        }
                        for(var i=0;i<arr.length;i++){
                            if(index==arr[i].b){
                                aRTli[arr[i].a-1].className='active';
                                aRBdiv[arr[i].a-1].style.display='block';    
                            }    
                        }
                    }else{//遍历找不到
                        var oLi=document.createElement('li');
                        oLi.innerHTML='<span id="close">X</span>'+aLi[index].innerHTML+'';
                        for(var i=0;i<aRTli.length;i++){
                            aRTli[i].className='';    
                        }
                        oLi.className='active';
                        oRT.appendChild(oLi);
                        //创建div
                        var oRBdiv=document.createElement('div');
                        oRBdiv.innerHTML='内容'+(index)+'';
                        for(var i=0;i<aRBdiv.length;i++){
                            aRBdiv[i].style.display='none';
                        }
                        oRBdiv.style.display='block';
                        oRB.appendChild(oRBdiv);
                        arr.push({a:aRTli.length,b:index});
                        oLi.n=(arr[aRTli.length-1]).a;
                        var oClose=oLi.children[0];
                        oClose.onclick=function(ev){
                            var oEvent=ev||event;
                            oRT.removeChild(this.parentNode);
                            oRB.removeChild(oRBdiv);
                            arr.splice((oLi.n-1),1);
                            for(var i=0;i<arr.length;i++){
                                arr[i].a=(i+1);
                                aRTli[i].n=i+1;
                            }
                            
                            //----------------------------------------开关
                            for(var i=0;i<aRTli.length;i++){
                                if(aRTli[i].className=='active'){
                                    bOk2=true;
                                }
                            }
                            if(!bOk2){
                                for(var i=0;i<aRBdiv.length;i++){
                                    aRBdiv[i].style.display='none';
                                    aRTli[i].className='';
                                }
                                for(var i=0;i<aLi.length;i++){
                                    aLi[i].className='';
                                }
                                aRTli[arr[arr.length-1].a-1].className='active';
                                aRBdiv[arr[arr.length-1].a-1].style.display='block';
                                aLi[arr[arr.length-1].b].className='on';
                            }
                            bOk2=false;
                            //选项卡
                            tab();
                            oEvent.cancelBubble=true;
                        };
                        //选项卡
                        tab();
                    }
                    bOk=false;
                    function tab(){
                        for(var i=0;i<aRTli.length;i++){
                            (function(index){
                                aRTli[index].onclick=function(){
                                    for(var i=0;i<aLi.length;i++){
                                        aLi[i].className='';
                                    }
                                    for(var i=0;i<aRTli.length;i++){
                                        aRTli[i].className='';
                                        aRBdiv[i].style.display='none';
                                    };
                                    this.className='active';
                                    aRBdiv[index].style.display='block';
                                    aLi[arr[index].b].className='on';
                                };
                            })(i);    
                        }
                    }
                }else{//没有length
                    var oLi=document.createElement('li');
                    oLi.innerHTML='<span id="close">X</span>'+aLi[index].innerHTML+'';
                    oLi.className='active';
                    oRT.appendChild(oLi);
                    //创建div
                    var oRBdiv=document.createElement('div');
                    oRBdiv.innerHTML='内容'+(index)+'';
                    oRB.appendChild(oRBdiv);
                    arr.push({a:1,b:index});
                    //删除
                    var oClose=oLi.children[0];
                    var oDiv=oRB.children[0];
                    oClose.style.display='none';
                }
            }
        })(i);    
    }
    
        
};
</script>
</head>

<body>
<div id="box">
    <ul id="l">
        <li>000</li>
        <li>111</li>
        <li>222</li>
        <li>333</li>
        <li>444</li>
        <li>555</li>
        <li>666</li>
        <li>777</li>
        <li>888</li>
        
    </ul>
    <div id="r">
        <ul id="rt">
            
        </ul>
        <div id="rb">
            
        </div>
    </div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值