2010-03-07 70 views
16

因此,我正在制作一个相当有问题的布局网站。用黑块表示四个角落图像TL,TR,BL和BR。深橙色区域是主要内容(宽度为960像素),外部区域用绿色箭头表示为浏览器窗口。见图:棘手的CSS布局

Diagram http://dotcafedesigns.com/stackoverflow/problem.gif

顶部图像表示在其最窄可能的位点 - 它不应该被允许比这窄的(960像素),如果它比定义的面积大,应该没有滚动条。底部的两张图片代表不同的浏览器宽度。

除非宽度降至960像素,否则底部左侧和右侧的黑块(图像)应始终位于屏幕的左下角和右侧,在这种情况下,BL和BR图像应插入主区域略。如果网站缩小到200px,BR图像不应该仍然在右上角。在这一点上,我并不真正关心它在IE6中的工作情况(我可以粗略地工作),但我甚至无法弄清楚如何完全没有Javascript或极其实验性的CSS。目前,我正在使用绝对定位的div来完成哪种工作,但工作不太正确。

我想我会愿意接受一点JS如果没有其他方式,但我宁愿不。

非常感谢!

+1

男人,你有一些疯狂的布局正在进行! :D – 2010-03-07 20:42:45

+0

我知道,真的让自己失败与这一个。 :( – Meep3D 2010-03-07 20:47:48

+0

注意:我会奖励这个问题,并将它提供给谁答案,你必须等待24小时左右,然后才发出赏金不幸(或者我现在会这样做),但我会等待那么久,做到这一点,然后标记你作为正确的答案。谢谢! – Meep3D 2010-03-07 20:49:25

回答

17

无需使用Javascript。至少在大多数现代浏览器中。使用简单的定位和一个@media查询我把它关闭:

<head> 
    <style type="text/css" media="screen"> 
    body { 
     margin: 0; 
     padding: 0; 
     font-family: sans-serif; 
     background: orange; 
     text-align: center; 
     color: black; 
     overflow-x: hidden; 
    } 
    #content { 
     width: 960px; 
     margin: 0 auto; 
     padding: 20px 0; 
     min-height: 800px; 
     background: #cc6600; 
     z-index: 0; 
    } 
    .image { 
     background: black; 
     color: white; 
     height: 60px; 
     width: 100px; 
     padding: 20px 0; 
     z-index: 1; 
     opacity: .95; 
    } 
    #top { 
     width: 960px; 
     margin: 0 auto; 
     position: relative; 
     overflow: visible; 
    } 
    #top .image { 
     position: absolute; 
    } 
    #top .left { 
     left: -80px; 
    } 
    #top .right { 
     right: -80px; 
    } 
    #bottom { 
     position: fixed; 
     bottom: 0; 
     width: 100%; 
     height: 100px; 
    } 
    #bottom .image { 
     position: absolute; 
    } 
    #bottom .left { 
     left: 0; 
    } 
    #bottom .right { 
     right: 0; 
    } 

    @media all and (max-width:1120px) { 

     #container { 
     width: 960px; 
     margin: 0 auto; 
     position: relative; 
     overflow: visible; 
     } 
     #bottom .left { 
     left: -80px; 
     } 
     #bottom .right { 
     right: -80px; 
     }  

    } 
    </style> 
</head> 
<body> 
    <div id="top"> 
    <div class="left image">left image</div> 
    <div class="right image">right image</div>  
    </div> 
    <div id="content"> 
    content 
    </div> 
    <div id="bottom"> 
    <div id="container"> 
     <div class="left image">left image</div> 
     <div class="right image">right image</div> 
    </div> 
    </div> 
</body> 

可能属于你的“极端实验CSS”的描述下,但我想你会被它确实多少支持有惊讶,并使用JS轻松启动它很容易 - 只需在重新调整大小时检查浏览器宽度,并在宽度低于1120px时添加额外的CSS。

+0

是的,媒体查询是要走的路。支持是除IE之外的所有现代浏览器,但如前所述,很容易检查媒体查询支持,然后添加js。 – 2010-03-09 08:09:30

+0

真的很酷,但你如何使它在IE下工作? – van 2010-03-16 00:41:48

+1

这里有一个选项:http://www.protofunc.com/scripts/jquery/mediaqueries/ - 另一种选择是在条件注释中添加一些JS来检查浏览器窗口宽度(例如:$(window) .resize(function(){if $(window).width()> 1120px {...做这个...};});用jQuery)并根据需要更改CSS(例如'$('#container ').css('width':'960px','margin':'0 auto');'再次使用jQuery)。 http://api.jquery.com/css/ – 2010-03-16 07:35:07

1

唯一真正棘手的部分是底部。它的日休息是非常简单的:

<body> 
    <div id="container"> 
     <div id="header"> 
      <div class="right"></div> 
      <div class="left"></div> 
     </div> 

     <div id="footer"> 
      <div class="right"></div> 
      <div class="left"></div> 
     </div> 
    </div> 
</body> 

CSS:

#container { 
    margin: 0 auto; 
    width: 960px; 
} 

#header { 
    position: relative; 
} 

#header .right, #header .left { 
    position: absolute; 
    width: 100px; 
} 

#header .right { 
    left: 940px; /* leaves 20px inside the #header */ 
    background: url(images/header_right.gif) no-repeat; 
} 

#header .left { 
    left: -80px; /* leaves 20px inside the #header */ 
    background: url(images/header_left.gif) no-repeat; 
} 

而对于#footer一些类似的东西,但你需要使用JavaScript的一点点检测窗口宽度和稍微调整left

+0

我有类似的东西 - 问题是正绝对定位#header .right导致滚动条。我甚至可以忍受这一点,但是脚注不会打球。 :( – Meep3D 2010-03-07 20:51:54

+0

@ meep3d - 我无法测试这个,但试着在body标签上设置'overflow-x:hidden;',它可以通过隐藏元素(或元素的一部分)来摆脱滚动条的问题在body元素之外),这听起来反直觉,但它可能工作,如果没有,张贴一些代码,所以我们可以采取一个裂缝 – davethegr8 2010-03-08 05:52:22

+0

不会隐藏所有的水平滚动条? – Meep3D 2010-03-08 08:32:22

2

是这样的吗?如果你愿意的话,你可以用类似的JQuery代码来代替JavaScript代码,只是想说明这个如何工作。

<html> 
<head> 
    <title>....</title> 

    <style type="text/css"> 
    html 
    { height: 100%; margin: 0; background: white; } 
    body 
    { height: 100%; width: 960px; margin: 0 auto; background: gray; overflow: hidden; } 
    #main 
    { margin: 0px; padding: 5px 20px; position: relative; } 

    #headLeft 
    { position: absolute; top: 0px; left: -80px; 
    width: 100px; height: 35px; background: green; } 
    #headRight 
    { position: absolute; top: 0px; right: -80px; 
    width: 100px; height: 35px; background: green; } 
    #footLeft 
    { position: absolute; bottom: 0px; left: 0px; 
    width: 100px; height: 35px; background: green; } 
    #footRight 
    { position: absolute; bottom: 0px; right: 0px; 
    width: 100px; height: 35px; background: green; } 
    </style> 

    <script type="text/javascript"> 
    function checkSize (event) 
    { 
    var contentWidth = 960; 
    var minOuterBoxSize = 60; // maximum 100-60=40 px will be displayed inside the main area 

    if (window.innerWidth - contentWidth < minOuterBoxSize * 2) 
    { 
     var pos = ((minOuterBoxSize * 2) - window.innerWidth + contentWidth)/2; 
     document.getElementById('footLeft').style.left = '-' + pos; 
     document.getElementById('footRight').style.right = '-' + pos; 
    } 
    else 
    { 
     document.getElementById('footLeft').style.left = '0px'; 
     document.getElementById('footRight').style.right = '0px'; 
    } 
    } 
    </script> 
</head> 

<body onload="checkSize(event);" onresize="checkSize(event);"> 
<div id="main"> 
    <div id="headLeft">TL</div> 
    <div id="headRight">TR</div> 

    <p>Normal content</p> 
</div> 

<div id="footLeft">BL</div> 
<div id="footRight">BR</div> 
</body> 
</html> 
+0

将JavaScript放在'window.onload'事件中。 – 2010-03-07 21:53:24

+4

呃...它是在一个onload事件.. – poke 2010-03-07 21:57:31