一款基於jquery滑動後固定於頂部的導航

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

之前已為大家介紹了好多css3實現的導覽功能表。今天分享一款基於jquery滑動後固定於頂部的導航。這款導航的特點是初始位於頂部下面一百個像素,當滑鼠滾動時到下方,導航一直處於頂部。如下:

線上預覽   源碼下載

實現的代碼。

html代碼:

<h1>        Scroll down</h1>    <h2>        And watch the menu bar</h2>    <nav id="menu">        <h1 id="sitename">            MySite</h1>        <ul>            <li class="active"><a href="http://www.w2bc.com">Home</a></li>            <li><a href="http://www.w2bc.com">Blog</a></li>            <li><a href="http://www.w2bc.com">About</a></li>            <li><a href="http://www.w2bc.com">Contact</a></li>        </ul>    </nav>

css代碼:

  body        {            height: 2000px;            font-family: Roboto, sans-serif;            -webkit-font-smoothing: antialiased;        }                h1, h2        {            text-align: center;        }                h1        {            color: #222;        }                h2        {            color: #555;        }                nav#menu        {            margin-top: 50px;            background: #3498db;            text-align: center;            height: 50px;            width: 100%;        }                nav#menu ul        {            padding: 0;            background: white;            margin: 0 auto;            display: inline-block;            height: 50px;        }                nav#menu ul li        {            float: left;            padding: 0 20px;            background: #3498db;            list-style: none;            margin-right: 5px;            line-height: 50px;            height: 50px; /*   * This element will get an alternative style   * once we apply the "scrolled" class.   * We also want a nice animation so we can   * simply set CSS3 transitions for this.   */            transition: margin 0.1s ease-in-out;        }                nav#menu ul li:nth-child(1)        {            margin-left: 5px;        }                nav#menu ul li.active        {            background: #2980b9;        }                nav#menu ul li a        {            color: white;            text-decoration: none;            font-size: 1.3em;        }                nav#menu h1#sitename        {            font-size: 1.2em;            line-height: 50px;            margin: 0;            position: absolute;            left: 0.5em;            color: white;            opacity: 0; /*   * Same thing here as with the menu items.   * This element will be animated into its   * alt. state with the transition property   */            transition: opacity 0.1s ease-in-out;        }                        /********************************************* * Once the ‘scrolled‘ class is toggled  * we can set any alternative styling that we * want for the menu bar  *********************************************/                nav#menu.scrolled        {            margin: 0;            position: fixed;            top: 0;        }                nav#menu.scrolled ul li        {            margin: 0;        }                nav#menu.scrolled h1#sitename        {            opacity: 1;        }

js代碼:

        var menu = $(‘nav#menu‘);        var watcher = scrollMonitor.create(menu);        watcher.lock();        watcher.stateChange(function () {            $(menu).toggleClass(‘scrolled‘, this.isAboveViewport)        }); //@ sourceURL=pen.js

註:本文愛編程原創文章,轉載請註明原文地址:http://www.w2bc.com/Article/7501

一款基於jquery滑動後固定於頂部的導航

聯繫我們

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