简体   繁体   English

CSS布局问题-在包装器上放置侧边栏

[英]CSS Layout Issue - Position Sidebar Over Wrappers

This is driving me nuts but I'm trying to position a sidebar over page wrappers. 这让我发疯,但是我试图在页面包装上放置侧边栏。 The problem with absolute positioning is that the sidebar needs to be aligned with the corner of the header (blue). 绝对定位的问题是,边栏需要与页眉的角(蓝色)对齐。 I thought about JS to keep it in the current position, and also calculate the height but I wasn't sure if that was the most efficient method. 我考虑过将JS保持在当前位置,并计算高度,但是我不确定这是否是最有效的方法。 I'm open to ideas, Here's a link to JSFiddle (you'd have to zoom out), code below: 乐于接受想法,这是JSFiddle的链接(您必须缩小),以下代码:

<style type="text/css">
    *       {margin: 0 auto; text-align: center; font-size: 42px; color: #fff; font-weight: bold;}
    .global {width: 1200px; margin: 0 auto;}

    #slider {height: 354px; width: 100%; max-width: 1400px; min-width: 1200px; height: 100%; min-height: 354px; background-color: red;}
    #container      {position: relative; background-color: beige;}

    #headerWrapper  {height: 200px; background-color: aqua;}
    #header         {height: 200px; background-color: blue;}

    #contentWrapper {height: 300px; background-color: black;}
    #content        {height: 300px; background-color: pink;}

    #listingWrapper {height: 500px; background-color: green;}
    #listings       {height: 500px; background-color: orange;}

    #footerWrapper  {height: 200px; background-color: cyan;}
    #footer         {height: 200px; background-color: grey;}

    #sidebar        {background-color: yellow; width: 240px; height: 170%; position: absolute; left: 2.5%; top: 0;}
</style>

<div id="headerWrapper">
    <div id="header" class="global">
        Header
    </div>
</div>

<div id="container">
    <div id="sidebar">

    </div>

    <div id="slider">
        Slider
    </div>

    <div id="contentWrapper">
        <div id="content" class="global">
            Top Content
        </div>
    </div>

    <div id="listingWrapper">
        <div id="listings" class="global">
            Bot Content
        </div>
    </div>
</div>

<div id="footerWrapper">
    <div id="footer" class="global">
        Footer
    </div>
</div>

Theoritically, the sidebar would start at the top left corner of the dark blue header, over the slider, and stop at the footer. 从理论上讲,侧栏将从深蓝色标题的左上角开始,在滑块上方,并在页脚处停止。 It shouldn't leave the global width: 1200px; 它不应该离开全局width: 1200px; . With a position absolute, it muddies the waters, and if I add a relative container with a static width, I lose the other container background colors. 绝对位置会给水带来混乱,如果我添加一个具有静态宽度的相对容器,则会失去其他容器的背景色。

This is the solution I found but I'll leave it open to see if anybody else has an idea. 这是我找到的解决方案,但我将其保持打开状态以查看是否有人有想法。 What I'm doing is getting the offset of my header then moving the sidebar according to my headers offset left. 我正在做的是获取标头的偏移量,然后根据标头的偏移量将边栏向左移动。 I do this whenever the document loads or if the user resizes their screen. 每当文档加载或用户调整屏幕大小时,我都会这样做。

$(document).ready(function(){
    var offset = $('#header').offset();
    $('#sidebar').css('left', offset.left+'px');
});

$(window).resize(function(){
    var offset = $('#header').offset();
    $('#sidebar').css('left', offset.left+'px');
});

Here: http://jsfiddle.net/Jmh4B/2/ 此处: http//jsfiddle.net/Jmh4B/2/

 #sidebar   
 {
    left: 0;
 }

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

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