简体   繁体   English

如何将导航栏固定在顶部

[英]how to make the navbar fixed to top

I just created one page website and wanna make my navbar fixed. 我刚刚创建了一个网页网站,并希望修复导航栏。 At the moment it looks as the picture shows: 如图所示,此刻: 在此处输入图片说明

To do that, I floated the logo to the left, navbar to the right and set the position to absolute so that right and bottom could be set to zero. 为此,我将徽标浮动在左侧,导航栏浮动在右侧,并将位置设置为绝对,以便可以将右侧和底部设置为零。

Here my html and some of the css: 这是我的html和一些CSS:

<div class="navigation">
    <nav id="site-navigation" class="main-navigation" role="navigation">
        <div class="menu-menu-1-container">
            <ul id="primary-menu" class="menu">
                <li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-5 current_page_item menu-item-55"><a href="http://localhost/scentology/">Home</a></li>
                <li id="menu-item-107" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-107"><a href="#about">About</a></li>
                <li id="menu-item-144" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-144"><a href="#products-and-services">Products &#038; Services</a></li>
                <li id="menu-item-142" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-142"><a href="#scent-marketing">Scent Marketing</a></li>
                <li id="menu-item-145" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-145"><a href="#clients-and-industries">Clients &#038; Industries</a></li>
                <li id="menu-item-143" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-143"><a href="#contact">Contact Us</a></li>
            </ul>
        </div>      
    </nav>  
</div>

.navigation{
    height:40px; line-height:40px;
}
nav#site-navigation {
    position: absolute;
    right: 0;
    bottom: 0;
}

I don't now much about scripting yet but was trying to adjust this snippet: 我现在对脚本的了解还不多,但是正在尝试调整此代码段:

<script>
// Create a clone of the menu, right next to original.
$('.navigation').addClass('original').clone().insertAfter('.navigation').addClass('cloned').css('position','fixed').css('top','0').css('margin-top','0').css('z-index','500').removeClass('original').hide();
scrollIntervalID = setInterval(stickIt, 10);
function stickIt() {
    var orgElementPos = $('.original').offset();
    orgElementTop = orgElementPos.top;               
    if ($(window).scrollTop() >= (orgElementTop)) {
        // scrolled past the original position; now only show the cloned, sticky element.
        // Cloned element should always have same left position and width as original element.     
        orgElement = $('.original');
        coordsOrgElement = orgElement.offset();
        leftOrgElement = coordsOrgElement.left;  
        widthOrgElement = orgElement.css('width');
        $('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement).show();
        $('.original').css('visibility','hidden');
    } else {
        // not scrolled past the menu; only show the original menu.
        $('.cloned').hide();
        $('.original').css('visibility','visible');
    }
}
</script>

The menu was fixed,but the problem is that I cannot see it properly because of the position properties I assume.I had to change the bottom value to see it. 菜单是固定的,但是问题是由于假定的位置属性,我无法正确看到它。我必须更改最低值才能看到它。 So that will only work when I scroll to the bottom. 因此,这仅在我滚动到底部时才有效。 When I try to scroll back to top,the menu will not be visible. 当我尝试返回顶部时,该菜单将不可见。

How can I put the navbar fixed to the top when I scroll down and back to it's default position when I scroll up? 向下滚动时如何将导航栏固定在顶部,向上滚动时如何将其导航回默认位置?

Try to keep things as short and simple as possible. 尝试使事情尽可能简短。

Replace you scripting with the jquery code below where you simply check if the scrolling position is off set: 将您的脚本替换为下面的jquery代码,您只需在其中检查滚动位置是否未设置:

(function( $ ){     
   var navOffset = jQuery("nav").offset().top; 
   jQuery(window).scroll(function() {
       var scrollPos = jQuery(window).scrollTop();
        if (scrollPos >= navOffset) {
            jQuery("nav").addClass("fixed");
        } else {
            jQuery("nav").removeClass("fixed");
        }
    });
})(jQuery);

Whatever styling you decide t apply must be according to the .fixed class. 无论您决定采用哪种样式,都必须根据.fixed类。 Remember that the fixed class will be removed every time you scroll to the top. 请记住,每次您滚动到顶部时,固定类都会被删除。

So here is your css: 这是您的CSS:

.fixed{
    position:fixed !important;
    z-index: 9999;
    top: 4%;
    width: 100%;
    text-align: center;
}

Depending on your content,I guess you will just need to play around with your top percentages 根据您的内容,我想您只需要尝试最高的百分比

Add to your css the following: 将以下内容添加到您的CSS中:

.navigation{
height:40px; 
line-height:40px;
position:fixed;

And put the elements after your navigation class in a div called content 并将元素放在导航类之后的div中,称为content
Example: 例:

<div id='content'>
<!--Put your main stuff here!-->
</div>

the css for content : content的CSS:

#content {
padding-top: someValueInPixel;
}

check the height of your navigation and replace someValueInPixel with that value. 检查导航的高度,并用该值替换someValueInPixel

With my solution you don't need javascript to hold your navbar at the top and you don't need the nav#site-navigation css 使用我的解决方案,您不需要javascript即可将导航栏保持在顶部,也不需要nav#site-navigation CSS

Why position:fixed; 为什么定位:固定;
w3schools.com says: w3schools.com说:

    The element is positioned relative to the browser window

That means, your element is on the top even when you scroll 这意味着即使滚动,您的元素也位于顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM