简体   繁体   English

浮动div-可变高度和可变列-无间隙

[英]Floating divs - variable height and variable columns - with no gaps

I HAVE UPDATED WITH FIDDLES, SEE BOTTOM 我已经更新了文件,请查看底部

I am developing a wordpress theme to work on iphone and ipads and other webkit devices. 我正在开发一个wordpress主题,可在iphone和ipads以及其他Webkit设备上使用。

I am using media queries to change the width of divs depending on the size and orientation of the device. 我正在使用媒体查询来更改div的宽度,具体取决于设备的大小和方向。

These divs however float left, to fill the device screen. 但是这些div向左浮动,以填充设备屏幕。

My problem is that the floated div's height are variable. 我的问题是,浮动div的高度是可变的。 And the div's which are higher than some, are causing gap issues on the floated elements. 而div值高于某些值,则导致浮动元素出现间隙问题。

I need possible advice on a fix so there are never any gaps, and elements always float, even if there is a higher div in its path. 我需要有关修复的可能建议,这样就不会有任何空白,并且即使其路径中的div较高,元素也总是浮动。

Is there a possible way of equalizing the div heights to the highest div, the only problem with this is that it need to work with orientation changes on iPad. 是否有可能将div高度均等到最高div,唯一的问题是它需要在iPad上适应方向变化。 On iPhone it's not so bad because there is only one column. 在iPhone上还不错,因为只有一列。 But on iPad, portrait has 2 columns and landscape has 3 columns. 但是在iPad上,“纵向”有2列,而“风景”有3列。

Please see diagram below explaining my issue. 请参阅下面的图解释我的问题。



图表



CURRENT CSS 当前的CSS

/* GLOBAL STYLE ( which iPhone uses first - 1 column ) ----------- */

.module {
    display: block;
    overflow: hidden;
    width: 100%;
    float: left;
}

/* IPADS (PORTRAIT - 2 column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {

    .module {
        width: 50%;
    }

}

/* IPADS (LANDSCAPE - 3 Column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {

    .module {
        width: 33.33%;
    }

}

BASIC MARKUP 基本加价

<div>

    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>
    <div class="module"></div>

</div>





UPDATE - I HAVE CREATED PURE CSS FIDDLES TO SIMULATE MY PROBLEM 更新-我创建了纯CSS组件来模拟我的问题


The fiddle below simulates SMARTPHONES (PORTRAIT AND LANDSCAPE) - ONE COLUMN 下面的小提琴模拟了智能手机(肖像和风景)- 一栏

See style .module - width: 100% which simulates the appearance of this @media query 参见样式.module - width: 100%模拟此@media查询的外观

@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px)

http://jsfiddle.net/motocomdigital/kdgnP/1/ http://jsfiddle.net/motocomdigital/kdgnP/1/


The fiddle below simulates IPADS (PORTRAIT) - TWO COLUMN 下面的小提琴模拟了IPADS(肖像)- 两个栏

See style .module - width: 50% which simulates the appearance of this @media query 参见样式.module - width: 50% ,它模拟此@media查询的外观

@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {

http://jsfiddle.net/motocomdigital/kdgnP/2/ http://jsfiddle.net/motocomdigital/kdgnP/2/


The fiddle below simulates IPADS (LANDSCAPE) - THREE COLUMN 下面的小提琴模拟了IPADS(景观)- 三栏

See style .module - width: 33.33% which simulates the appearance of this @media query 参见样式.module - width: 33.33% ,它模拟此@media查询的外观

@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {

http://jsfiddle.net/motocomdigital/kdgnP/3/ http://jsfiddle.net/motocomdigital/kdgnP/3/


Like I said in a comment, you need to calculate this in javascript and use absolute positioning. 就像我在评论中说的那样,您需要使用javascript计算出来并使用绝对定位。 Here is an example: 这是一个例子:

http://jsfiddle.net/rJxtm/4/ http://jsfiddle.net/rJxtm/4/

//Generate random height for each module, normally this doesn't need to be done because
//they have various amounts of contents in a real app.
$(".module").each(function(i) {
    var height = (150 + (Math.random() * 100 | 0));
    this.style.height = height + "px";
});


$(window).bind("orientationchange resize", function(e) {
    var modules = $(".module"),
        columnWidth = modules[0].offsetWidth,
        margin = 10,
        parentWidth = $(".modules").width(),
        fits = Math.floor(parentWidth / (columnWidth + margin * 2)),
        columnHeights = {},
        i;

    for (i = 0; i < fits; ++i) {
        columnHeights[i] = 0;
    }

    function getTop(i) {
        var nColumn = i % fits;
        return columnHeights[nColumn];
    }

    modules.each(function(i) {
        var height = this.offsetHeight, //Now I am just retrieving the height
            nColumn = i % fits;

        this.style.left = ((columnWidth + margin) * nColumn) + "px";
        this.style.top = getTop(i) + "px";
        columnHeights[nColumn] += (height + margin);
    });

}).trigger("resize");

这不能简单地通过float来解决-唯一的选择是使所有这些模块的大小相同(或为较小的模块增加一些余量)

You can use JavaScript to set equal height to all of them. 您可以使用JavaScript将所有高度设置为相等。

This is the minified jQuery plugin: 这是缩小的jQuery插件:

(function(a){a.fn.equalHeight=function(){var b=0;this.each(function(){var c=a(this).height();c>b&&(b=c)});this.each(function(){a(this).height(b)})}})($);

And you can use like that: 您可以这样使用:

$('#modules').children().equalHeight();

Another good solution would be CSS display table. 另一个好的解决方案是CSS显示表。 Take a look at this post: http://css-tricks.com/fluid-width-equal-height-columns/ 看一下这篇文章: http : //css-tricks.com/fluid-width-equal-height-columns/

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

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