简体   繁体   English

导航菜单仅垂直跟随屏幕,而不水平跟随

[英]Navigation menu that only follows screen vertically not horizontally

You can see my problem on the image here: 您可以在此处的图像上看到我的问题:

在此处输入图片说明

When I minimized my website, I discovered that my contents flows over my navigation when I use the vertical scroll bar. 当我最小化我的网站时,我发现当我使用垂直滚动条时,我的内容会流过我的导航。 I know I could just use a z-index to make it go behind. 我知道我可以使用z-index使其落后。 But I want to be able to scroll to the right and see the rest of the page to the right, without scrolling the "content". 但是我希望能够向右滚动并在不滚动“内容”的情况下向右查看页面的其余部分。

Right now, this is only run locally. 目前,这仅在本地运行。 So I can't give you a link to the page. 所以我不能给你链接到页面。

Here's the stripped HTML: 这是剥离的HTML:

<div id="main-wrapper">
<div id="menu-wrapper">
    <div id="logo">
        <a href="index.html">
        </a>
    </div>
    <div id="nav">
    </div>
</div>
<div id="content-wrapper">
    <div id="sub-content">
    </div>
</div>

And the CSS for the menu and content area: 菜单和内容区域的CSS:

#menu-wrapper 
{
position: fixed;
width: 223px;
height: 100%;
}

#content-wrapper 
{
width: 698px;
min-height: 500px;
position: absolute;
left: 223px;
top: 41px;
}

I hope that you can help me out, and please let me know if you need any more information 希望您能为我提供帮助,如果您需要更多信息,请告诉我

position: fixed will make your ellement 'follow' you on page that's why the text is overlapping your left bar. position: fixed会使您的情绪在页面上“跟随”您,这就是文本与您的左栏重叠的原因。

Anyway, the following should work as you want: 无论如何,以下内容应该可以工作:

#menu-wrapper {
    position: fixed;
    width: 223px;
    height: 100%;
    z-index: 999;
}

#content-wrapper {
    width: 698px;
    min-height: 500px;
    padding-top: 41px;
    margin-left: 223px;
}

I would still set that z-index in case the user has javascript disabled. 我仍然会设置z-index,以防用户禁用了javascript。 Let the content hide under the menu. 让内容隐藏在菜单下。 Best solution in worst case scenario. 在最坏情况下的最佳解决方案。

Make sure you have jquery loaded. 确保已加载jQuery。 If not, add this into your header: 如果不是,请将其添加到标题中:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

Now add this just before </body> : 现在在</body>之前添加它:

$( document ).ready(function() {
    $(window).scroll(function() {
        $('#menu-wrapper').css({'position' : 'absolute', 'top': $(window).scrollTop()});
    });
});

Here is a Fiddle for better demonstration. 这是一个小提琴,用于更好的演示。

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

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