简体   繁体   English

如何使固定位置菜单旁边的内容在较小的分辨率下不重叠

[英]How to make content next to a fixed-position menu not overlap in smaller resolutions

I'm trying to figure out how to make it so if a user has a browser window that's under about 1024px (the site has no horizontal-scroll at 1024px+), if they do scroll right to see more of the main content, that it does not get overlapped/messy'd by the left fixed-position menu. 我正在尝试弄清楚如何做到这一点,如果用户的浏览器窗口小于1024px(网站没有以1024px +的水平滚动),并且用户确实向右滚动以查看更多主要内容,不会被左侧的固定位置菜单重叠/混乱。

I've made a JS fiddle that recreates the basic problem I am facing: http://jsfiddle.net/YE7ZZ/1/ 我做了一个JS小提琴,它重新创建了我面临的基本问题: http : //jsfiddle.net/YE7ZZ/1/

CSS CSS

#wrap {
    width:100%;
    background-image:url('../images/Imagine/bg_image44.png');
    background-attachment:fixed;
} 

#top {

}

#left {
    position:fixed;
    border:1px solid red;
    background:pink;
    width:250px;
}
#positioner {
    margin-left:250px;
    width:auto;
}
#content {
    border:1px solid green;
    width:700px;
    margin:auto;
        background:grey;

}

HTML HTML

    <div id="wrap">
    <div id="top"></div>
    <div id="left">menu item~~~~~<br /><br /><br />menu item~~~~~<br /><br /><br />menu item~~~~~<br /><br /><br />menu item~~~~~<br /><br /><br />menu item~~~~~<br /><br /><br />menu item~~~~~<br /><br /><br /></div>
    <div id="positioner">
        <div id="content">asdf content that should be lower-resolution browser friendly and not be overlapped by the menu<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

            asdf content that should be lower-resolution browser friendly and not be overlapped by the menu<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
            asdf content that should be lower-resolution browser friendly and not be overlapped by the menu<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
            asdf content that should be lower-resolution browser friendly and not be overlapped by the menu<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
        </div>
    </div>
</div>

I have attempted to solve this issue about three separate times over the course of the past six weeks and have not been able to find a fix, so any help would be appreciated so much. 在过去的六个星期中,我尝试了大约三个不同的时间来解决此问题,但未能找到解决方法,因此,我们将不胜感激。

Thank you for your time. 感谢您的时间。

edit-- my ideal solution would be that a horizontal scroll-bar appears for the content portion, so that they can scroll through the content itself, without having to: 1) overlap the left menu; 编辑-我的理想解决方案是在内容部分显示一个水平滚动条,以便他们可以滚动浏览内容本身,而不必:1)重叠左侧菜单; or 2) cut off the amount of content viewable; 或2)切断可见内容的数量; or 3) reduce the size of the left menu 或3)缩小左侧菜单的大小

SOLVED: thank you so much to @Gaby aka G. Petrioli 求助:非常感谢@Gaby aka G. Petrioli

I used this javascript solution: 我使用了以下javascript解决方案:

$(document).ready(function(){
var lastLeft = -1,
    menu = $('.left_, .top_');

$(window).on('scroll resize', function(){
    var left = $(window).scrollLeft();
    if (left >= 0 && left!==lastLeft){
        lastLeft = left;
        menu.css('left',-left+'px');
    }
});

}); });

And changed the CSS as he outlined, and on my live version, had to change the positioning from some top menu elements from fixed to absolute. 并按照他的概述更改了CSS,在我的实时版本中,不得不将某些顶级菜单元素的位置从固定更改为绝对。 Thank you so much everyone! 谢谢大家!谢谢!

Not sure of your specific needs but you could remove the width:700px from the .content rule and so that element will shrink as the viewport does.. 不确定您的特定需求,但是可以从.content规则中删除width:700px ,以便该元素将像视口一样缩小。

Demo at http://jsfiddle.net/YE7ZZ/2/ 演示在 http://jsfiddle.net/YE7ZZ/2/


If on the other hand you need to maintain the layout, and you want the fixed to apply only for vertical scrolling, you will have to use some jquery ( not possible with pure CSS ) 另一方面,如果您需要维护布局,并且只想将fixed应用于垂直滚动,则必须使用一些jquery( 纯CSS不能实现

var lastLeft = -1,
    menu = $('#left');
$(window).on('scroll resize', function(){
    var left = $(window).scrollLeft();
    if (left >= 0 && left!==lastLeft){
        lastLeft = left;
        menu.css('left',-left+'px');
    }
});

Demo at http://jsfiddle.net/YE7ZZ/3/ 演示在 http://jsfiddle.net/YE7ZZ/3/

Change the CSS based on the width of the screen/viewport. 根据屏幕/视口的宽度更改CSS。

Add this to your head: 将此添加到您的头上:

<meta name="viewport" content="initial-scale=1">

Have your css rules changed based on size: 根据大小更改css规则:

@media screen and (min-width : ###px) and (max-width : ###px) {
   .your-css-here{
   }
}

http://css-tricks.com/snippets/css/media-queries-for-standard-devices/ http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

In doing this, you can change your fixed nav menu to display differently. 为此,您可以将固定的导航菜单更改为不同的显示方式。

IE: IE:

@media screen and (max-width : 1024px) {
   nav{
      position:relative;
      etc...:...;
   }
 }

Edit--- Had max/min-device-width . 编辑--具有max/min-device-width Needs to be max/min-width . 需要为max/min-width

Not sure it will be exactly what you want/need but you could as well use absolute positioning for the div with id="positioner". 不确定是否正是您想要/需要的,但是您也可以使用id =“ positioner”为div使用绝对定位。 Simply replace your margin-left:250px by 只需将您的margin-left:250px替换为

position: absolute;
left: 250px;
right: 0;
overflow: auto;

I modified a little bit the Fiddle. 我对小提琴做了一些修改。 You can find the new version here 您可以在这里找到新版本

You can do this with floats. 您可以使用浮点数执行此操作。 You may need to use a clearfix if you want the container (#wrap) to pick up the heights. 如果希望容器(#wrap)抬起高度,则可能需要使用clearfix。

There is a fiddle here: http://jsfiddle.net/EqXBp/ 这里有一个小提琴: http : //jsfiddle.net/EqXBp/

CSS: CSS:

#left {
    float: left;
    border:1px solid red;
    background:pink;
    width:250px;
}

#content {
    border:1px solid green;
    float: right;
    width: calc(100% - 4px);
    margin:auto;
    background:grey;
}

It's a pretty simple fix now that I understand what you're wanting. 现在,我知道您想要的是一个非常简单的修复程序。 Just have to make your positioner wrapper absolute and set its markers and tell it to scroll. 只需将positioner器包装器设为绝对,并设置其标记并告诉其滚动即可。

 #positioner {
     position: absolute;
     top: 0px;
     left: 250px;
     right: 0px;
     bottom: 0px;
     overflow: auto;
 }

First you must understand how fixed position works. 首先,您必须了解固定位置的工作原理。 It is a position relative to the screen's viewport and it doesn't move when scrolled. 它是相对于屏幕视口的位置,并且在滚动时不会移动。

The following solution styles your left box relatively but makes it behave as fixed. 以下解决方案相对地设置了左框的样式,但使其表现为固定的。

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

#wrapp {
  min-width: 955px;
}
#left {
  position: relative;
  float: left;
  width: 250px;
  top: 0;
}
#positioner {
  position: relative;
  float: left;
  margin-left: 0;
}

Use this Javascript program to keep the left menu on the top: 使用此Javascript程序将左侧菜单保持在顶部:

<script>
function scrollTop() { return document.body.scrollTop || document.documentElement.scrollTop; }

$( document ).ready(function() {
    $(window).scroll(function() {    
        $("#left").css({ top: scrollTop() + 'px' });
    });    
});
<script/>

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

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