js+css實現tab菜單轉場效果的方法_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了js+css實現tab菜單轉場效果的方法。分享給大家供大家參考。具體實現方法如下:

index.css如下:

複製代碼 代碼如下:
* { 
    margin: 0px; 
    padding: 0px; 

body { 
    width: 600px; 
    margin: 0 auto; 
    background-color: silver; 

 
#contanier { 
    background-color: yellow; 
    width: 600px;height: 400px; 

 
#tabNavi { 
    width: 600px;height: 30px; 
    background-color: #00bfff; 
    text-decoration: none; 
    list-style-type: none; 

 
#tabNavi li { 
    float: left; 
    margin-right: 7px; 
     background-color: #008b8b; 
    color: white; 
    cursor: pointer; 
    width: 60px; 
    height: 28px; 
    line-height: 30px; 
    text-align: center; 

#content { 
    width: 600px;height: 370px; 
    background-color: white; 
}

index.html如下:

複製代碼 代碼如下:
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>js實現tab菜單動態轉場效果</title> 
    <link href="css/index.css" rel="stylesheet" /> 
    <script type="text/javascript"> 
        function gel(id) { 
            return document.getElementById(id); 
        } 
 
        var arr = [ 
            { "title": "新聞", "content": "這是新聞頻道" }, 
            { "title": "財經", "content": "這是財經頻道" }, 
            { "title": "娛樂", "content": "這是娛樂頻道" }, 
            { "title": "體育", "content": "這是體育頻道" }, 
            { "title": "汽車", "content": "這是汽車頻道" }, 
            { "title": "視頻", "content": "這是視頻頻道" }, 
            { "title": "生活", "content": "這是生活頻道" } 
        ]; 
 
        window.onload = function() { 
            for (var i = 0; i < arr.length; i++) { 
                var liNew = document.createElement("li"); 
                liNew.innerHTML = arr[i].title; 
                gel("tabNavi").appendChild(liNew); 
                //在這些li上面都綁定點擊事件,就需要給他們每一個賦一個屬性(最好是id) 
                liNew.setAttribute("id", i); 
                 
                liNew.onclick = function () { 
                   var navs = gel("tabNavi").childNodes; 
                    //清除所有顏色 
                   for (var j = 0; j < navs.length; j++) { 
                       if (navs[j].nodeType == 1) { 
                           navs[j].style.backgroundColor ="#008b8b"; 
                       } 
                    } 
 
                    this.style.backgroundColor = "red"; 
                    gel("content").innerHTML = arr[this.id].content; 
                }; 
            } 
        }; 
    </script> 
</head> 
<body> 
    <div id="contanier"> 
        <ul id="tabNavi"></ul> 
        <div id="content" class="content"></div> 
    </div> 
</body> 
</html>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.