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>选项卡</title>
    <style type="text/css">
    *{margin:0px;padiving:0px;}
    body{ padiving:0;font:12px "宋体"; height:100%; width:100%; }
    ul,li{ list-style-type:none;padding:0px;}
    .tabHeads{width:962px;height:31px;margin:100px auto 0px;background-color:#31E2B7; }
    .tabContents{width:960px;height:auto;margin:0px auto; border:1px solid #333;padding:0px; }
    .tabContents div{height:198px;line-height:25px;width:auto;padding:5px;overflow:auto;background-color:#eff;}
    .tabs{width:100px;height:25px;line-height:25px;margin:5px 2px 0px;border:1px solid #333;border-BOTTOM:none;text-align:center;display:inline;float:left;}
    .curTab{color:red;background-color:#eff; MARGIN-BOTTOM: -2px; POSITION: relative; height:26px;}
 </style>
</head>
<body>
      <div class="tabHeads" id='tabHeads'>
            <ul>
                <li style="margin-left:50px;">新闻1</li>
                <li >新闻2</li>
                <li >新闻3</li>
                <li >新闻4</li>
                <li >新闻5</li>
                <li >新闻6</li>
            </ul>
        </div>
        <div id="tabContents" class="tabContents">
            <div >
                <textarea   rows=3 cols="20"></textarea>新闻1
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20">234656</textarea>新闻2a2
            </div>
            <div style="display:none;" >
                <textarea  rows=3 cols="20"></textarea>新闻3a3
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20"></textarea>新闻4
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20">234656</textarea>新闻5
            </div>
            <div  style="display:none;">
                <textarea  rows=3 cols="20"></textarea>新闻6
            </div>
        </div>
        
     <div class="tabHeads" id='tabHeads1'>
            <ul>
                <li style="margin-left:50px;">新闻1</li>
                <li >新sdfgsdfg闻2</li>
                <li >新闻3</li>
                <li >新sdfg闻4</li>
                <li >新sdfgs闻5</li>
                <li >新sdfg闻6</li>
            </ul>
        </div>
        <div id="tabContents1" class="tabContents">
            <div >
                <textarea   rows=3 cols="20"></textarea>新闻1
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20">234656</textarea>新闻2a2
            </div>
            <div  style="display:none;">
                <textarea  rows=3 cols="20"></textarea>新闻3a3
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20"></textarea>新闻4
            </div>
            <div style="display:none;">
                <textarea   rows=3 cols="20">234656</textarea>新闻5
            </div>
            <div style="display:none;" >
                <textarea  rows=3 cols="20"></textarea>新闻6
            </div>
        </div>

</body>
<script type="text/javascript"></script>
<script type="text/javascript">


if(typeof(HTMLElement) != "undefined")  // 给非IE 浏览器添加方法 contains
{
    HTMLElement.prototype.contains = function(obj)
    {   
      while(obj != null &&  typeof(obj.tagName) != "undefined")   
      {
            if(obj == this)
              return true;
            obj = obj.parentNode;
      }    
            return false;   
    };   
}

var $ = function (id,obj) {  
    return 'string' == typeof(id) ? (obj||document).getElementById(id) : id;   
};  
var $$ = function (name,obj) {  
    return 'string' == typeof(name) ? (obj||document).getElementsByTagName(name):name;   
};  

//选项卡类,头,内容,选中的索引,选中样式,默认样式,是否自动切换,切换时间间隔(毫秒)
function Tab(heads,contents,selectedIndex,curStyle,style,isAutoChange,space)
{
    if(heads && contents && heads.length && contents.length &&  heads.length==contents.length)
    {
        this.heads=heads;
        this.contents=contents;
    }else
    {
        alert("参数错误!");
        return;
    }
    this.selectedIndex = selectedIndex||0;
    this.curStyle=curStyle||"";
    this.style = style||"";
    this.isAutoChange=isAutoChange||false;
    this.space = space || 3000;
    this.timer=null;
    this.init();
}

Tab.prototype.init=function()
{
    var obj = this;
    for(var i=0;i<obj.heads.length;i++)
    {
        obj.heads[i].index=i;
        obj.heads[i].οnmοuseοver=function(){obj.setTab(this.index);if(obj.isAutoChange){clearInterval(obj.timer);}};
        if(obj.isAutoChange)
        {
             obj.contents[i].οnmοuseοut=obj.heads[i].οnmοuseοut=function(e){
                e = e||event;
                e = e.toElement || e.relatedTarget ; // IE toElement  FF:relatedTarget
               if(typeof(e)!="undefined" &&!this.contains(e)){clearInterval(obj.timer);obj.timer=setInterval(function(){obj.setTab(obj.selectedIndex=(++obj.selectedIndex)%obj.heads.length);},obj.space);}
            };

            obj.contents[i].οnmοuseοver=function(e){clearInterval(obj.timer);};
        }
    }
    if(obj.isAutoChange){obj.timer=setInterval(function(){obj.setTab(obj.selectedIndex=(++obj.selectedIndex)%obj.heads.length);},obj.space);}
    obj.setTab( obj.selectedIndex);
}
Tab.prototype.setTab=function(index)
{
     for(var i=0;i<this.heads.length;i++)
    {
        this.heads[i].className = i == index ? this.style+' '+this.curStyle :this.style;
        this.contents[i].style.display = i == index ? "block" : "none";
    }
    this.selectedIndex = index;
}


window.οnlοad=function()
{
    var heads = $("tabHeads");
    var contents=$("tabContents");
    var tab1=new Tab($$('li',heads),getChilds(contents),0,'curTab','tabs');

    var heads1 = $("tabHeads1");
    var contents1=$("tabContents1");
    var tab2=new Tab($$('li',heads1),getChilds(contents1),1,'curTab','tabs',true,1500);
   
};

function getChilds(o)
{
    if(!o || !o.childNodes)return null;
    var r=[];
    for(var i=0;i<o.childNodes.length;i++){
      if(o.childNodes[i].tagName){r.push(o.childNodes[i]);}
    }
    return r;
}

</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HTML和JavaScript实现多滑动门选项卡特效,以下是一个简单的示例: HTML代码: ``` <div class="tab-container"> <div class="tab-header"> <ul> <li onclick="openTab('tab1')">选项卡1</li> <li onclick="openTab('tab2')">选项卡2</li> <li onclick="openTab('tab3')">选项卡3</li> </ul> </div> <div class="tab-content"> <div id="tab1" class="tab-pane">选项卡1的内容</div> <div id="tab2" class="tab-pane">选项卡2的内容</div> <div id="tab3" class="tab-pane">选项卡3的内容</div> </div> </div> ``` CSS代码: ``` .tab-container { width: 500px; height: 300px; border: 1px solid #ccc; } .tab-header { border-bottom: 1px solid #ccc; } .tab-header ul { list-style: none; margin: 0; padding: 0; } .tab-header li { float: left; padding: 10px; border-right: 1px solid #ccc; cursor: pointer; } .tab-header li:last-child { border-right: none; } .tab-header li.active { background-color: #ccc; } .tab-content { height: 250px; overflow: hidden; } .tab-pane { padding: 10px; height: 100%; } ``` JavaScript代码: ``` function openTab(tabName) { var i, tabContent, tabLinks; tabContent = document.getElementsByClassName("tab-pane"); for (i = 0; i < tabContent.length; i++) { tabContent[i].style.display = "none"; } tabLinks = document.getElementsByTagName("li"); for (i = 0; i < tabLinks.length; i++) { tabLinks[i].className = tabLinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; event.currentTarget.className += " active"; } ``` 这个示例中,我们使用了一个包含选项卡头部和内容的DIV容器。选项卡头部使用一个无序列表来实现,每个列表都有一个onclick事件,当点击列表时,触发openTab函数。选项卡内容使用DIV标记来实现,每个内容区域都有一个唯一的ID,用于在openTab函数中切换显示。CSS样式用于美化选项卡的外观。JavaScript代码用于控制选项卡的切换和样式的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值