16

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

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

顶部图像以尽可能窄的方式表示站点 - 如果它大于定义的区域,则不应允许它比此 (960 像素) 窄,则不应有滚动条。底部的两张图片代表不同宽度的浏览器。

左下角和右下角的黑色块(图像)应始终位于屏幕的左下角和右下角,除非宽度下降到 960px,在这种情况下,BL 和 BR 图像应该稍微戳入主区域。如果站点缩小到 200 像素,则 BR 图像不应该仍然在右上角。

在这一点上,我并不真正关心它在 IE6 中的工作原理(我可以让它大致工作),但我什至无法弄清楚如何在没有 Javascript 或极其实验性的 CSS 的情况下完全做到这一点。目前我正在使用绝对定位的 div 的哪种工作,但工作不太正确。

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

回答非常感谢!

4

3 回答 3

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 来引导它是多么容易 - 只需检查浏览器宽度即可-size 并在宽度低于 1120 像素时添加额外的 CSS。

于 2010-03-09T08:01:15.503 回答
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>
于 2010-03-07T21:41:51.173 回答
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稍微调整s。

于 2010-03-07T20:48:30.673 回答