简体   繁体   English

固定顶部菜单和内容滚动

[英]Fixed top menu scrolling along with content

I'm attempting to make a fixed top menu. 我正在尝试制作固定的顶层菜单。 On a viewport > 1170px (which is my minimum page viewport here), everything is ok. 在大于1170px的视口(这里是我的最小页面视口)上,一切正常。 On viewport less than 1170px, main content of the page is horizontally scrollable (ok), but #main-menu is displayed only up to current window height, so the last items are hidden. 在小于1170px的视口上,页面的主要内容可以水平滚动(确定),但是#main-menu仅显示到当前窗口的高度,因此隐藏了最后一项。

Is is possible to make this menu fixed and to act like a part of the scrollable content? 是否可以使此菜单固定充当可滚动内容的一部分?

The site will be not responsive. 该网站将没有响应。

HTML: HTML:

<nav id="main-menu">
    <div class="container">
        <ul class="nav">
            <li id="item1"><a href="#">Menu item 1</a></li>
            <li id="item2"><a href="#">Menu item 2</a></li>
            <li id="item3"><a href="#">Menu item 3</a></li>
            <li id="item4"><a href="#">Menu item 4</a></li>
            <li id="item5"><a href="#">Menu item 5</a></li>
        </ul>
    </div>
</nav>

<div id="content">
    <div class="container">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce bibendum odio et interdum pretium.</p>
    </div>
</div>

CSS 的CSS

body {
  background-color: #000;
  color: #fff;
  font-size: 17px;
  min-width: 1170px;
}
.container {
  width: 1170px;
}
#main-menu {
  position: fixed;
  height: 68px;
  width: 100%;
  display: block;
  top: 0px;
  z-index: 500;
}

#content {
  position: relative;
  top: 150px;
}

DEMO: http://jsfiddle.net/zyqkrwtk/1/ 演示: http : //jsfiddle.net/zyqkrwtk/1/

I know it is partially solvable by setting overflow-x: auto to menu, but there will be extra scrollbar, which is not desirable. 我知道可以通过将overflow-x: auto设置为菜单来部分解决,但是会有额外的滚动条,这是不可取的。

EDIT: Sorry, I forgot to mention important thing - the page content is actually very long (it will be one-page microsite), so the fixed position is therefore desirable. 编辑:抱歉,我忘了提到重要的事情-页面内容实际上很长(它将是一页微型站点),因此固定位置是可取的。 Updated demo - http://jsfiddle.net/fvgf2mj6 更新的演示-http : //jsfiddle.net/fvgf2mj6

You have unnecessary uses of the CSS rule position . 您有不必要的CSS规则position用法。

I fixed your code here http://jsfiddle.net/nuc4s6h9/ Basically I removed the style for #content element and the position:relative for #main-menu element. 我在这里固定了代码http://jsfiddle.net/nuc4s6h9/基本上,我删除了#content元素的样式和#main-menu元素的position:relative Out of the box this 2 elements have display:block which made them render one below the other. 开箱即用,这2个元素具有display:block ,使它们在另一个下方渲染。

Let me know if that is what you wanted for. 让我知道这是否是您想要的。

Ok, so I'm coming back with my solution! 好的,我将返回我的解决方案! I have inspired in this similar question Centering a fixed element, but scroll it horizontally 我启发了这个类似的问题, 将固定元素居中,但水平滚动

Basically everything I had to change was 基本上我要做的就是

#main-menu {
position: absolute
...

and add this little jQuery code to set menu position and width when user scrolls/resizes window: 并添加以下jQuery小代码以在用户滚动/调整窗口大小时设置菜单位置和宽度:

var alignMenu = function() {
    $('#main-menu').css({
        'top': $('body').scrollTop(),
        'width': $('#vystuduj-to').width()
    })
}

$(window).scroll(function(){ alignMenu() });
$(window).resize(function(){ alignMenu() });

DEMO: http://jsfiddle.net/LorLe7jx/ 演示: http : //jsfiddle.net/LorLe7jx/

Thanks to everybody for pointing out to right direction! 感谢大家指出正确的方向!

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

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