导航位置固定在滚动wordpress上

时间:2013-11-26 20:45:12

标签: php jquery html css wordpress

我想知道如何实现类似于本网站的效果。当您向下滚动网站时,徽标会消失但菜单仍然是固定的:

http://www.nowness.com/

这是我的网站:http://www.itsalifestylething.co.uk/quick-cupcake-catch-up/

PHP

<body <?php body_class(); ?>>
<div id="page" class="hfeed site">
    <header id="masthead" class="site-header" role="banner">

        <hgroup>
            <h1 class="site-title"><img src="http://www.itsalifestylething.co.uk/wp-content/uploads/2013/11/logo-large.png" /></h1>
            <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
        </hgroup>

        <nav id="site-navigation" class="main-navigation" role="navigation">

            <h3 class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></h3>
            <a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentytwelve' ); ?>"><?php _e( 'Skip to content', 'twentytwelve' ); ?></a>
            <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>

        </nav><!-- #site-navigation -->

        <?php if ( get_header_image() ) : ?>
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php header_image(); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
        <?php endif; ?>
    </header><!-- #masthead -->

    <div id="main" class="wrapper">

CSS

/* Header
------------------------------------------------------------ */
.site-header {
    padding: 5px 0 !important;
}

hgroup {
    width: 100%;
}

#masthead {
    width: 960px;
    margin: 0 auto;
    position: relative;
} 

h1.site-title {
    margin: 0 auto;
    width: 480px;
    height: 123px;
}

.social img {
    margin-right: 15px;
}

/* Nav
------------------------------------------------------------ */
.main-navigation {
    width: 100%;
    margin: 0px !important;
}

.main-navigation div.nav-menu > ul {
    border: 0px !important;
    margin: 0 auto;
    width: 430px;
}

.main-navigation li {
    margin: 0 25px 0 0;
}

.main-navigation li ul {
    padding-top: 2px;
    z-index: 10;
}

.main-navigation li a:hover, .main-navigation .current_page_item > a {
    color: #33d898;
    font-weight: normal;
}

.main-navigation li ul {
    -webkit-box-shadow: 0px 5px 10px 1px rgba(0,0,0,0.2);
    box-shadow: 0px 5px 10px 1px rgba(0,0,0,0.2);
    width: 200px;
}

.main-navigation li ul li a {
    background: #ffffff;
    border-bottom: 1px solid #e7e7e7
}

.main-navigation li ul li a:hover {
    background: #ffffff;
    color: #33d898;
}

.nav-menu {
    border-bottom: 1px solid #e7e7e7;
}

3 个答案:

答案 0 :(得分:1)

你必须使用javascript添加位置:固定到你的标题。你还需要给你的身体一个补偿。

nav-menu-static {
   position: fixed;
   top: 0;
   background: #fff;
   width:960px
}

答案 1 :(得分:1)

这就是我使用htmlcssjQuery的方式。

首先,这是我订购html的方式,

<nav>
   <span class="logo">Your Logo</span>
   <ul>
       <li id="logo">Your Logo</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
       <li>Link</li>
   </ul>
</nav>

接下来是css

html, body {
  margin: 0;
  padding: 0;
}

ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

nav {
  width: 80%;
  background: #c1c1c1;
  position: fixed;
  top: 0;
  left: 50%;
  margin-left: -40%;
}

nav li {
  display: inline-block;
  cursor: pointer;
}

#logo {
 display: none; 
}

#logo.scroll {
  display: inline-block;
}

.logo {
  font-size: 50px;
  font-family: "century gothic";
}

最后是jQuery

的一点点
$(window).scroll(function() {
   $('.logo').hide();
   $('#logo').addClass('scroll');

   if ($(window).scrollTop() === 0) {
       $('.logo').show();
       $('#logo').removeClass('scroll'); 
   }
});

这是一个jsfiddle,Link

答案 2 :(得分:0)

#masthead {
  background: none repeat scroll 0 0 #FFFFFF;
  margin: 0 auto;
  position: fixed;
  width: 960px;
}

然后为.site-content类设置margin-top