English 中文(简体)
如何根据窗户的宽度将一栏放在中心
原标题:how to center columns based on window width
<link rel="stylesheet" type="text/css" href="reset.css" />      
<style type="text/css">
        body { position: relative; }

        .contain { position:relative; overflow: hidden; width: 80%; height: 100%; margin: 0 auto; }     

        .box {
            background-color: #F0F0F0;
            color: #888;
            font-family: Arial, Tahoma, serif;
            font-size: 13px; }

        .box p { padding: 10px; }

        .box span {
            float: left;
            font-size: 26px;    
            font-weight: bold;  }

        div.alt { background-color: #CCC; }
</style>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    var myFluidGrid = {
            COLNUMBER : 2, // Minimum column number.
            COLMARGIN : 10, // Margin (in pixel) between columns/boxes.
            COLWIDTH : 240, // Fixed width of all columns.

            doLayout : function() {
                var self = this;
                var pointer = 0;
                var arr = [];
                var columns = Math.max(this.COLNUMBER, parseInt($( body ).innerWidth() / (this.COLWIDTH + this.COLMARGIN)));

                $( .box ).css( position ,  absolute ).css( width , this.COLWIDTH  +  px );
                $( .box ).each(function() {
                    var tempLeft = (pointer * (self.COLWIDTH + self.COLMARGIN));
                    $(this).css( left , tempLeft +  px );

                    var tempTop = 0;
                    if (arr[pointer]) { tempTop = arr[pointer]; }
                    $(this).css( top , tempTop +  px );

                    arr[pointer] = tempTop + $(this).outerHeight() + self.COLMARGIN;
                    pointer++;
                    if (pointer === columns) { pointer = 0; }
                });
            }
        };
        $(window).ready(function() {
            myFluidGrid.doLayout();
        }).resize(function() {
            myFluidGrid.doLayout();
        });
    </script>

<div class="contain">    
    <div class="box">This is box number 1...</div> 
    <div class="box">This is box number 2...</div>
    <div class="box">This is box number 3...</div>
</div>

现行法典:rel=“nofollow” http://grahamthomas.me/temp/test.html

无论大小,都设法使上述电网留在窗户中。 目前,我有大致集中的,但当窗户面积得到调整时,在新一栏有内容(即动态电网在三栏之间,但四栏之间)之前,中心就变得不便。

我在联合材料中并不重要,但我的逻辑是:

  • create a 100% wrapper (which would mimic the body.innerWidth dimension in the JS)
  • create a centered wrapper inside this (80% for example)
  • place the content in the centered wrapper
  • once the 100% wrapper is large enough to handle an additional column, append the new div within the centered wrapper

你们可以清楚地看到过度流通:当把窗户拖走时,隐藏的财产被压在最右箱上。 我从此假设,窗户的宽度是适当计算的。 我尝试了各栏的变量,例如:

var columns = Math.max(this.COLNUMBER, parseInt(($( body ).innerWidth() * 0.8)/ (this.COLWIDTH + this.COLMARGIN)));

存放在窗户内,但依然没有合适的中心。

在我看来,有人对此有想法?

增 编

问题回答

不需要 Java文:

<html>
<head>
<style>
body {
    text-align: center;
}
div.main {
    position: absolute;
    top: 100px;
    left: 50%;
    margin-left: -40%;
    width: 80%;
}

div.main div.block {
    height: 180px;
    width: 180px;
    background: #a00;
    float: right;
    margin: 10px;
}
</style>
</head>
<body>
<div class="main">
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
</div>
</body>
</html>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签