我正在使用 masonry + bootstrap 并注意到当我有一个 3 列网格时我有 10 个元素,它会显示一个底部有 2 个空格的 3x4 网格。我怎么能在底部自动添加 2 个空的 div 来填充它而不是那些空白?那么总的 div 将变为 12,其中 2 个 div 只是空白?
这不应该固定为 3 列,但只要检测到有 N 个可以填充的空 div,就应该动态添加空 div。应该适用于加载和调整大小。
.item 大小不会有问题,因为它们都具有相同的宽度和高度(方框/正方形类型)
我做了一个jsFiddle现在可以在最后一行的空白处添加填充符。这也适用于使用 layoutComplete 事件调整大小。但问题是,每当我调整大小时,它都会不断添加新的填充物。
尝试调整为不同的尺寸,您会发现它不断添加填充物。
以防万一,这也是代码。
HTML
<input type="hidden" name="hfTotalGridItems" id="hfTotalGridItems" value="10" />
<div class="grid">
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
<div class="item">
<div>lorem</div>
</div>
</div>
<div class="result"></div>
CSS
.grid {
margin: 0 auto;
}
.item {
margin-bottom: 20px;
border: 1px solid red;
height: 80px;
width: 80px;
}
.filler {
background-color: #999;
border: 1px solid blue;
}
JQuery
$(function () {
function calculateRows() {
var lisInRow = 0;
var $item = $('.grid .item');
var $grid = $('.grid');
var itemWidth = $('.grid .item').width();
var itemHeight = $('.grid .item').height();
$item.each(function () {
if ($(this).prev().length > 0) {
if ($(this).position().top != $(this).prev().position().top) return false;
lisInRow++;
} else {
lisInRow++;
}
});
var lisInLastRow = $item.length % lisInRow;
if (lisInLastRow == 0) lisInLastRow = lisInRow;
$('.result').html('No: of lis in a row = ' + lisInRow + '<br>' + 'No: of lis in last row = ' + lisInLastRow);
if (lisInLastRow < lisInRow) {
var $clonedItem = $('.grid .item:last-child').clone().empty().css({
width: itemWidth,
height: itemHeight
}).addClass('filler');
$grid.append($clonedItem).masonry('appended', $clonedItem);
} else {
if (newTotal > $('#hfTotalGridItems').val()) {
$grid.masonry('remove', $('.grid .item.filler'));
$grid.masonry();
}
}
}
var $grid = $('.grid');
$grid.masonry({
itemSelector: '.item',
isFitWidth: true,
gutter: 20
});
$grid.masonry('on', 'layoutComplete', function (event) {
calculateRows(event.length);
});
$grid.masonry();
});
最佳答案
你需要检查两件事
(originalBoxs % lisInRow === 0)。如果框数大于允许的最大框数。您可以计算最大允许框如下所示
var totalAllowed = lisInRow;
while (totalAllowed < originalBoxs) {
totalAllowed += lisInRow;
}
如果这是真的,那么您应该删除所有多余的框。否则添加填充框。这是更新的 jsFiddle
我在下面添加了更新的 jQuery 代码
$(function () {
// remember the original box lenth.
var $item = $('.grid .item');
var originalBoxs = $item.length;
function calculateRows() {
var lisInRow = 0;
var $item = $('.grid .item');
var $grid = $('.grid');
var itemWidth = $('.grid .item').width();
var itemHeight = $('.grid .item').height();
// calculate how many boxes are in the first row.
$item.each(function () {
if ($(this).prev().length > 0) {
if ($(this).position().top != $(this).prev().position().top) return false;
lisInRow++;
} else {
lisInRow++;
}
});
// calculate how many boxes are in the last row.
var lisInLastRow = $item.length % lisInRow;
$('.result').html('No: of lis in a row = ' + lisInRow + '<br>' + 'No: of lis in last row = ' + lisInLastRow);
// the total allowed boxes on the page.
var totalAllowed = lisInRow;
while (totalAllowed < originalBoxs) {
totalAllowed += lisInRow;
}
if (($item.length > originalBoxs && originalBoxs % lisInRow === 0) || ($item.length > totalAllowed)) {
// if the number of original boxes evenly divides into the number of boxes in a row.
// or the number of boxes on the page is past the max allowed.
// remove any filler boxes.
var boxesToDelete = $('.grid .item.filler');
var deleteBoxes = $item.length - totalAllowed;
for (var i = 0; i < deleteBoxes; i++) {
// delete unnesecary boxes.
$grid.masonry('remove', boxesToDelete[boxesToDelete.length - i - 1]);
}
} else if (lisInLastRow !== 0) {
// if the last row does not equal 0 and the number of boxes is less then the original + the first row
// then fill it in with new boxes.
var $clonedItem = $('.grid .item:last-child').clone().empty().css({
width: itemWidth,
height: itemHeight
}).addClass('filler');
$grid.append($clonedItem).masonry('appended', $clonedItem);
}
}
var $grid = $('.grid');
$grid.masonry({
itemSelector: '.item',
isFitWidth: true,
gutter: 20
});
$grid.masonry('on', 'layoutComplete', function (event) {
calculateRows(event.length);
});
$grid.masonry();
});
关于javascript - 当网格底部有空白时,Bootstrap + Masonry 添加空白 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141840/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
我正在运行Ubuntu11.10并像这样安装Ruby1.9:$sudoapt-getinstallruby1.9rubygems一切都运行良好,但ri似乎有空文档。ri告诉我文档是空的,我必须安装它们。我执行此操作是因为我读到它会有所帮助:$rdoc--all--ri现在,当我尝试打开任何文档时:$riArrayNothingknownaboutArray我搜索的其他所有内容都是一样的。 最佳答案 这个呢?apt-getinstallri1.8编辑或者试试这个:(非rvm)geminstallrdocrdoc-datardoc-da
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=