---
title: "仿花瓣网浮动在顶层的导航菜单代码"
author: "ssxxjjii"
date: "2012-12-08T17:14:37Z"
tags: []
categories: ["互联网"]
slug: "untitled"
draft: false
lastmod: "2012-12-08T17:14:37Z"
id: 1744199
url: "https://tdcq.iteye.com/blog/1744199"
---

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt;html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
&lt;title&gt;仿花瓣的导航效果&lt;/title&gt;  
&lt;style type="text/css"&gt;  
    body,h1,ul{margin:0;}  
    ul li{list-style-type:none;}  
    #header{width:100%;border-top:solid 1px #ccc;border-bottom:solid 1px #ccc;text-align:center;}  
    h1{padding:10px 0;color:#D74452;}  
    .nav{width:1000px;background:#fff;margin:20px auto 0;border:solid 1px #ccc;zoom:1;border-radius:5px;box-shadow:0 1px 6px rgba(0,0,0,0.1);color:#D74452;}  
    .nav\_scroll{position:fixed;width:100%;margin:0;left:0;top:0;}  
    .nav:after{content:"";display:block;height:0;clear:both;visibility:hidden;}  
    .nav ul li{float:left;margin:0 20px;height:30px;line-height:30px;}  
    .nav ul li a{cursor:pointer; }  
    .nav ul li a:hover{text-decoration:underline;}  
    h2{height:400px;line-height:400px;}  
&lt;/style&gt;  
&lt;script src="[http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js](http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js)" type="text/javascript"&gt;&lt;/script&gt;  
&lt;script type="text/javascript"&gt;  
    $(document).ready(function(){  
                var topMain=$("#header").height()+20//是头部的高度加头部与nav导航之间的距离。  
                var nav=$(".nav");  
                $(window).scroll(function(){  
                    if ($(window).scrollTop()&gt;topMain){//如果滚动条顶部的距离大于topMain则就nav导航就添加类.nav\_scroll，否则就移除。  
                        nav.addClass("nav\_scroll");  
                    }  
                    else  
                    {  
                        nav.removeClass("nav\_scroll");  
                    }  
                });  
      
        })  
&lt;/script&gt;  
&lt;/head&gt;  
      
&lt;body&gt;  
&lt;div id="header"&gt;&lt;h1&gt;花瓣&lt;/h1&gt;&lt;/div&gt;&lt;!--header--&gt;  
&lt;div class="nav"&gt;  
    &lt;ul&gt;  
        &lt;li&gt;&lt;a&gt;关注&lt;/a&gt;&lt;/li&gt;  
        &lt;li&gt;&lt;a&gt;最新&lt;/a&gt;&lt;/li&gt;  
        &lt;li&gt;&lt;a&gt;最热&lt;/a&gt;&lt;/li&gt;  
        &lt;li&gt;&lt;a&gt;视频&lt;/a&gt;&lt;/li&gt;  
        &lt;li&gt;&lt;a&gt;家居&lt;/a&gt;&lt;/li&gt;  
        &lt;li&gt;&lt;a&gt;旅行&lt;/a&gt;&lt;/li&gt;  
    &lt;/ul&gt;  
&lt;/div&gt;&lt;!--nav--&gt;  
&lt;div style="background:#f9f9f9;color:#000;" id="cont"&gt;  
    &lt;h2&gt;1&lt;/h2&gt;  
    &lt;h2&gt;2&lt;/h2&gt;  
    &lt;h2&gt;3&lt;/h2&gt;  
    &lt;h2&gt;4&lt;/h2&gt;  
    &lt;h2&gt;5&lt;/h2&gt;  
    &lt;h2&gt;6&lt;/h2&gt;  
    &lt;h2&gt;7&lt;/h2&gt;  
    &lt;h2&gt;8&lt;/h2&gt;  
    &lt;h2&gt;1&lt;/h2&gt;  
    &lt;h2&gt;2&lt;/h2&gt;  
    &lt;h2&gt;3&lt;/h2&gt;  
    &lt;h2&gt;4&lt;/h2&gt;  
    &lt;h2&gt;5&lt;/h2&gt;  
    &lt;h2&gt;6&lt;/h2&gt;  
    &lt;h2&gt;7&lt;/h2&gt;  
    &lt;h2&gt;8&lt;/h2&gt;  
    &lt;h2&gt;1&lt;/h2&gt;  
    &lt;h2&gt;2&lt;/h2&gt;  
    &lt;h2&gt;3&lt;/h2&gt;  
    &lt;h2&gt;4&lt;/h2&gt;  
    &lt;h2&gt;5&lt;/h2&gt;  
    &lt;h2&gt;6&lt;/h2&gt;  
    &lt;h2&gt;7&lt;/h2&gt;  
    &lt;h2&gt;8&lt;/h2&gt;  
    &lt;h2&gt;1&lt;/h2&gt;  
    &lt;h2&gt;2&lt;/h2&gt;  
    &lt;h2&gt;3&lt;/h2&gt;  
    &lt;h2&gt;4&lt;/h2&gt;  
    &lt;h2&gt;5&lt;/h2&gt;  
    &lt;h2&gt;6&lt;/h2&gt;  
    &lt;h2&gt;7&lt;/h2&gt;  
    &lt;h2&gt;8&lt;/h2&gt;  
    &lt;h2&gt;1&lt;/h2&gt;  
    &lt;h2&gt;2&lt;/h2&gt;  
    &lt;h2&gt;3&lt;/h2&gt;  
    &lt;h2&gt;4&lt;/h2&gt;  
    &lt;h2&gt;5&lt;/h2&gt;  
    &lt;h2&gt;6&lt;/h2&gt;  
    &lt;h2&gt;7&lt;/h2&gt;  
    &lt;h2&gt;8&lt;/h2&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;