使用javascript實現簡單的選項卡切換,javascript選項卡

來源:互聯網
上載者:User

使用javascript實現簡單的選項卡切換,javascript選項卡

代碼相當簡潔、簡單易懂,就不多廢話了。

直接奉上代碼:

複製代碼 代碼如下:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html" charset="utf-8">
        <title>js簡單選項卡</title>
        <script type="text/javascript" src="js/demo.js"></script>

        <style type="text/css">
            *{ font-size: 14px;margin: 0px;}
    a{color:#a0b3d6;text-decoration: none;}
    .tabs{border:1px solid #a0b3d6;margin:100px;width:350px;}
    .tabs-nav a{background:#eaf0fd; line-height:30px;padding:0px 20px 0px 20px;display:inline-block;border-right:1px solid #a0b3d6;border-bottom:1px solid #a0b3d6; float:left;}
    .tabs-nav .on{background:white; border-bottom:1px solid white; position:relative;}
    .tabs-content{display: block; padding:20px;border-top:1px solid #a0b3d6; margin-top:-1px;}
    .tabs-content_hide{display: none;}
        </style>

    </head>
    <body>
        <div class="tabs" id="tabs">
            <h2 class="tabs-nav clearfix">
                <a href="javascript:;" class="on">首頁</a>
                <a href="javascript:;">技術</a>
                <a href="javascript:;">生活</a>
                <a href="javascript:;">作品</a>
            </h2>
            <div style="clear:both;"></div>
            <p class="tabs-content">首頁</p>
            <p class="tabs-content_hide">技術</p>
            <p class="tabs-content_hide">生活</p>
            <p class="tabs-content_hide">作品</p>
        </div>
   
    </body>
<footer></footer>
</html>

------demo.js---------------

複製代碼 代碼如下:
window.onload=function(){
    tabs("tabs","mouseover");
}
function tabs(id,trigger){
    var tabBtn = document.getElementById(id).getElementsByTagName("h2")[0].getElementsByTagName("a");
    var tabsContent = document.getElementById(id).getElementsByTagName("p");
    for(var i=0;i<tabBtn.length;i++){
        tabBtn[i].index = i;
        if(trigger=='mouseover'){
            tabBtn[i].onmouseover=function(){
                clearClass();
                this.className="on";
                showContent(this.index);
            }
        }
        function showContent(n){
            for (var i=0; i<tabsContent.length ;i++) {
                tabsContent[i].index = i;
                tabsContent[i].className = "tabs-content_hide";
        }
        tabsContent[n].className="tabs-content";
    }
        function clearClass(){
            for(var i=0;i<tabBtn.length;i++){
                tabBtn[i].className="";
            }
        }
    }
}

是不是很簡單就實現了選項卡的轉場效果呢,小夥伴們自己美化下就可以用到自己項目中去了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.