2014-10-30 62 views
0

我开始一个单独的线程here关于使用浮动构建完全流畅的响应式布局的不同方法,并且我正在考虑一个完全不同的解决方案。 I have started a new fiddle here,其中我已将布局块分成两个主列,即.left-column.right-column。我想知道,如果通过jQuery,我可以在某个断点处删除这些列容器 - 比如@ 678px--强制所有的div线性化@ 100%的宽度,并重新排列它们,使得在这个模型中,它们是从1到8用jQuery重新排列布局块?

有序顺序可以这样容易地与jQuery实现?另外,我也愿意接受任何其他(如css)的建议,只要达到这个预期的效果。我发现漂浮的所有元素在我的其他试图证明问题太多呈现(我目前的执行至少)

感谢任何帮助这里。与bootstrap帮助

+0

你检查http://masonjs.com/或类似的库。 – cforcloud 2014-10-30 04:30:41

+0

哇,我曾试过jQuery砌体,发现行为太多,但没有听说过梅森;你觉得这很容易处理? – nickpish 2014-10-30 04:33:56

+0

你的情况相当简单。两者都应该为你工作。总是有更多的尝试http://jquery-plugins.net/category/grid-layout – cforcloud 2014-10-30 04:40:53

回答

1

还有一列基于网格的解决方案。 在这里,您可以通过使用Grid system

Fiddle is here来调整列宽,如果您想玩。

$(function(){ 
 

 
$.fn.itemify = function(update){ 
 

 
var $t = $(this); 
 

 
if ($t.data('itemify') && update!==true) return this; 
 

 
var options = { col:'.col', item:'.item' }, 
 

 
items = $t.find(options.item), 
 
col_count = 0, 
 

 
fn = function(){ 
 
    var cols = $t.find(options.col+':visible'); 
 
    if(cols.length == col_count) return; 
 
    
 
    console.log(cols.length, col_count); 
 
    col_count = cols.length; 
 
    
 
    $t.find(options.col).empty(); 
 
    
 
    items.each(function(i, t){ 
 
    \t shortest(cols).append(t); 
 
    }); 
 
}, 
 

 
shortest = function(cols){ 
 
var a = 0, h = -1; 
 
cols.each(function(i,t){ 
 
    if($(t).height() < h || h==-1) { 
 
     h = $(t).height(); 
 
     a = i; 
 
    } 
 
    //console.log(a, b, h, $(this).height()); 
 
}); 
 
return cols.eq(a); 
 
}; 
 

 
$(window).unbind('resize', fn).bind('resize', fn); 
 
fn(); 
 

 
return this.data('itemify', 1); 
 
}; 
 

 

 
/* ----- testing - START item + window resizing --*/ 
 
var test_items = $('#itemify .item'); 
 
$('.test').removeClass('hide').find('button').click(function(){ 
 
    var col = $('#itemify .col').eq(0).append(test_items), len = $('#itemify .item').length; 
 
    
 
    for(i=0; i<10; i++) 
 
    col.append($('<div class="item">'+ ++len +'</div>').height(Math.random()*300+20)); 
 
    
 
    test_items = $('#itemify .item'); 
 
    test(); 
 
    $('#itemify').itemify(true); 
 
    $('html, body').animate({ scrollTop: $(document).height() }, 100); 
 
}); 
 
function test(){ 
 
$('.test .txt').text($('#itemify .item').length + ' items'); 
 
    
 
$('#itemify .item').each(function(){ 
 
    var $t = $(this); 
 
    
 
    if($t.find('textarea').length==0) { 
 
    $t.html($('<textarea>'+ $t.text() +'</textarea>').height($t.height()-5)); 
 
    $t.removeAttr('style').removeClass().addClass('item'); 
 
    } 
 
}); 
 
} 
 
test(); 
 
/* ----- testing - END -- */ 
 

 

 
/// start itemify 
 
$('#itemify').itemify(); 
 

 
});
body { 
 
    font-family: sans-serif; 
 
    font-weight: bold; 
 
    text-align: center; 
 
} 
 
.container-fluid { 
 
    padding: 0 !important; 
 
} 
 
.row#itemify { 
 
    margin: 0 0 0 15px; 
 
} 
 
.row .col { 
 
    padding: 15px 15px 0 0; 
 
} 
 
.item { 
 
    margin-bottom: 15px; 
 
    background-color: #AAA; 
 
} 
 
.one { 
 
    height: 170px; 
 
} 
 
.two { 
 
    height: 250px; 
 
} 
 
.three { 
 
    height: 150px; 
 
} 
 
.four { 
 
    height: 250px; 
 
} 
 
.five { 
 
    height: 450px; 
 
} 
 
.six { 
 
    height: 350px; 
 
} 
 
.seven { 
 
    height: 250px; 
 
} 
 
.eight { 
 
    height: 200px; 
 
} 
 
.nine { 
 
    height: 500px; 
 
} 
 

 
/* testing */ 
 
.test { 
 
    position: fixed; 
 
    bottom: 8px; 
 
    right: 10px; 
 
    text-align: right; 
 
    font-size: 80%; 
 
} 
 
.item textarea { 
 
    border: medium none; 
 
    resize: vertical; 
 
    min-width: 100%; 
 
    max-width: 100%; 
 
    text-align: center; 
 
    background-color: transparent; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container-fluid"> 
 
    <div id="itemify" class="row"> 
 
    <div class="col col-xs-12 col-sm-4 col-md-3 col-lg-2"> 
 
     <div class="item one">1</div> 
 
     <div class="item two">2</div> 
 
     <div class="item three">3</div> 
 
     <div class="item four">4</div> 
 
     <div class="item five">5</div> 
 
     <div class="item six">6</div> 
 
     <div class="item seven">7</div> 
 
     <div class="item eight">8</div> 
 
     <div class="item nine">9</div> 
 
     <div class="item five">10</div> 
 
     
 
     <!-- more items --> 
 
     <div class="item six">11</div> 
 
     <div class="item two">12</div> 
 
     <div class="item four">13</div> 
 
     <div class="item eight">14</div> 
 
     <div class="item three">15</div> 
 
     <div class="item one">16</div> 
 
     <div class="item nine">17</div> 
 
     <div class="item seven">18</div> 
 
     <div class="item two">19</div> 
 
     <div class="item eight">20</div> 
 
    </div> 
 
    <div class="col col-sm-8 col-md-4 col-lg-2 visible-sm visible-md visible-lg"></div> 
 
    <div class="col col-md-5 col-lg-3 visible-md visible-lg"></div> 
 
    <div class="col col-lg-5 visible-lg"></div> 
 
    </div> 
 
</div> 
 

 
<!-- testing --> 
 
<div class="test hide"> 
 
    <div class="txt">txt</div> 
 
    <button>add 10 items</button> 
 
</div>

+0

哇,谢谢cforcloud,这是一个非常有趣的解决方案;可能是迄今为止我见过的最好的。我正在尝试使用freetile.js作为一种可能的解决方案,但我仍然遇到类似于石工的一些不稳定的行为 - 如果您愿意查看我的测试页面[here](http:// nickpish.com/_dev/freetile/) - 看看你对发生了什么有什么想法,我会非常感激。我喜欢这种方法的简单性,但我仍然将这些元素放入单个列中;然而,在调整浏览器窗口的大小时,div将会正确对齐(?) – nickpish 2014-11-05 02:05:56

+0

@nickpish我想这就是它的工作原理。你可以登录你的[问题到freetile](https://github.com/yconst/Freetile/issues)。我只是[更新了小提琴](http://jsfiddle.net/cforcloud/qmaagw3r/9/)。 – cforcloud 2014-11-07 06:16:27