jquery實現簡單的導覽列轉場效果($(this).index)

來源:互聯網
上載者:User

標籤:margin   add   children   微軟   index   推薦   line   hid   cursor   

  一個簡單的導覽列轉場效果的製作,主要通過索引值和jquery的siblings()來實現

 html代碼:

<div class="container">    <ul class="top-nav">        <li class="nav">html</li>        <li class="nav">css</li>        <li class="nav">javascript</li>    </ul>        <ul class="bottom-content">        <li class=content>html是基礎</li>        <li style="display: none" class="content">css也是基礎</li>        <li style="display: none" class="content">javascirpt是進階</li>    </ul></div>

  css代碼(自己隨便寫的樣式,大家可以自由發揮):

        *{        margin: 0;        padding: 0;    }    li{        list-style: none;        display: inline-block;    }    .container{        width: 240px;    }    .top-nav{        font-size: 0;    }    .nav{        width: 80px;        height: 30px;        line-height: 30px;        font-size: 16px;        font-family: "微軟雅黑";        text-align: center;        cursor: pointer;    }    .nav:hover{        background-color: #CC6699;        color:#fff;    }    .content{        width: 240px;        text-align: center;    }    

  js代碼(要記得先引入jquery哦):

$(‘.nav‘).click(function(){
$(
this).parent().next().children().eq($(this).index()).show().siblings().hide();
})
//通過$(this)找到點擊的DOM元素
//通過parent(),next()這些選取器找到要實現顯示隱藏的元素的父元素
//通過children().eq(
$(this).index())來用索引進行控制,$(this).index()就是你點擊元素的索引,只要把元素一一對應就好了
//通過jquery的.show()方法顯示對應的元素
//通過.sibling()找到同級的兄弟元素,進行隱藏

tips:你也可以用addClass()和removeClass()給對應的元素加上一個類,實現想要的效果

  以上方法對於一般的情境應該足夠使用,如果層級關係過於複雜就不推薦使用了

jquery實現簡單的導覽列轉場效果($(this).index)

聯繫我們

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