简体   繁体   English

响应式“类砌体”列,不使用砌体(尝试避免位置:绝对;)

[英]Responsive “masonry-like” columns without using masonry (trying to avoid position:absolute;)

I have a layout of boxes that are all floated left and when you click on their headings, they slide open revealing content. 我有一个框的布局,所有框都向左浮动,当您单击其标题时,它们会滑动打开以显示内容。 The issue is that the way floats work, when you click to expand one of them, it messes with the row underneath. 问题是浮动方式起作用,当您单击以展开其中之一时,它会与下面的行混淆。

http://jsfiddle.net/FCCye/ <-- click on one of the headings to see the issue. http://jsfiddle.net/FCCye/ <-单击标题之一以查看问题。

I've solved this by separating them into columns like so: 我通过将它们分成几列来解决了这一问题:

http://jsfiddle.net/caW4M/ http://jsfiddle.net/caW4M/

That works fine, however, the layout needs to be responsive, so when the window is 480 or lower, it needs to be 1 column. 效果很好,但是布局需要响应,因此当窗口为480或更低时,它需要为1列。 Between 480 and 768 it needs to be 2 columns. 在480和768之间,它需要2列。 Anything above 768, 3 columns. 768以上的任何东西,3列。 (obviously, the jsfiddles don't show the breakpoints I have set up.) (显然,jsfiddles不显示我设置的断点。)

This is the code I've come up with to solve this, however it is not working at all. 这是我想出的代码来解决这个问题,但是它根本无法正常工作。 I was wondering if someone could tell me what I'm doing wrong. 我想知道是否有人可以告诉我我做错了什么。

// Create all three portfolio columns
    var one = $('<div/>').addClass('column').addClass('one');
    var two = $('<div/>').addClass('column').addClass('two');
    var three = $('<div/>').addClass('column').addClass('three');

    // Store all portfolio elements into variables once they're in columns
    var colElems = $('.column .project');

    // Now append the columns
    var winWidth = $(window).width();
    if ( winWidth > 480 && winWidth <= 768 ) {

        // Remove everything from columns and delete existing columns
        $(colElems).appendTo('#portfolio .content');
        $('#portfolio .content').remove(one,two,three);

        // Store portfolio elements into variables for safe-keeping
        var c1Elems = $('.project:nth-child(2n+1)');
        var c2Elems = $('.project:nth-child(2n+2)');

        // Perform appends into portfolio columns
        c1Elems.appendTo(one);
        c2Elems.appendTo(two);

        // Append portfolio elements to columns     
        $('#portfolio .content').append(one,two);

    }else{

        // Remove everything from columns and delete existing columns
        $(colElems).appendTo('#portfolio .content');
        $('#portfolio .content').remove(one,two,three);

        // Store portfolio elements into variables for safe-keeping
        var c1Elems = $('.project:nth-child(3n+1)');
        var c2Elems = $('.project:nth-child(3n+2)');
        var c3Elems = $('.project:nth-child(3n+3)');

        // Perform appends into portfolio columns
        c1Elems.appendTo(one);
        c2Elems.appendTo(two);
        c3Elems.appendTo(three);

        $('#portfolio .content').append(one,two,three); 
    }

So, what I'm trying to do is append the normal 3 columns when it's not between 480 and 768 (because on mobile size, the columns would stack on top of each other anyway) and when between 480 and 768, only append two columns. 所以,我想做的是在不介于480和768之间的情况下追加正常的3列(由于移动大小,这些列无论如何都会堆叠在一起),而当介于480和768之间时,仅追加两列。 So my thought is that at the different sizes, I would have to pull all of the boxes out of the columns, delete the columns, and reappend the columns in different numbers based on the window width. 所以我的想法是,在不同的尺寸下,我将不得不从列中拉出所有框,删除列,然后根据窗口宽度以不同的数字重新添加列。 This has proved to be a failed attempt, so if anyone can explain to me what I'm doing wrong I would be very appreciative! 事实证明,这是一次失败的尝试,因此,如果有人可以向我解释我做错了什么,我将非常感激!

Thanks! 谢谢!

Not an answer to your question, but if you will follow the advice then your question will no longer be of interest. 这不是您问题的答案,但是如果您遵循建议,那么您的问题将不再有意义。 ;-) First of all why don't you use CSS3 Media Queries for your different layouts? ;-)首先,为什么不对不同的布局使用CSS3 Media Queries? That is what they are for. 那就是他们的目的。

Secondly it is "bad practice" to use pixel values (or any other kind of absolute units) for defining breakpoints, even if a lot of authors actually do! 其次,即使很多作者确实这样做,使用像素值(或任何其他种类的绝对单位)来定义断点也是“错误的做法”! It is best to only use relative units like 'em'. 最好只使用“ em”之类的相对单位。

The new Flexbox module could also be an option for you, depending on the supported Browser versions (especially IE 8). 您还可以选择使用新的Flexbox模块,具体取决于受支持的浏览器版本(尤其是IE 8)。

And why don't you let the expanded box not simply overlap the other content by using 'position: absolute'? 为什么不通过使用“ position:absolute”不让扩展框简单地与其他内容重叠呢?

Well, doing things with Javascript which should be done with pure CSS isn't a good idea. 嗯,使用纯CSS来完成Javascript操作并不是一个好主意。 What happens, when JS is deactivated? 禁用JS后会发生什么? And also from a performance point of view, on resizing the viewport ... - all bad. 而且从性能的角度来看,调整视口的大小...-都不好。

So my advice would be to completely rethink your approach. 因此,我的建议是完全重新考虑您的方法。

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

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