我已经按照Bootstrap示例来实现thumbnails with captions。但是,我的标题文本具有不同的长度,导致三个缩略图的高度不同。这看起来很难看。为了解决这个问题,我尝试了matchHeight jQuery插件,但这没有效果。
这是我的代码:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.matchHeight-min.js" type="text/javascript"></script>
<script type="text/javascript">
$(".thumbnail").matchHeight({ byRow: true, });
</script>
</head>
<body>
...
<div class="row">
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="img/img-1.jpg" alt="">
<div class="caption">
<h4>Heading 1</h4>
<p>...</p>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="img/img-2.jpg" alt="">
<div class="caption">
<h4>Heading 2</h4>
<p>...</p>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="img/img-3.jpg" alt="">
<div class="caption">
<h4>Heading 3</h4>
<p>...</p>
</div>
</div>
</div>
</div>
...
</body>
我在这里缺少什么?如何让这三个缩略图很好地对齐?
答案 0 :(得分:1)
尝试使用js:
(function() {
$(function() {
$('.thumbnail').matchHeight({
byRow: true,
property: 'height',
target: null,
remove: false
});
});
})();
答案 1 :(得分:1)
.thumbnail {
height: 547px;
overflow-y: hidden;
}
.img-responsive {
margin-bottom: 30px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="http://cdn.makeuseof.com/wp-content/uploads/2012/01/8bit_mushroom_intro.jpg?92a7a3" alt="">
<div class="caption">
<h4>Heading 1</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit maximus nulla id eleifend. Curabitur sit amet tellus vitae orci condimentum aliquam. Aenean facilisis lacinia suscipit. Cras quam massa, pharetra a convallis nec,
gravida eu metus. Phasellus gravida faucibus nulla, sit amet suscipit purus. Donec luctus, ligula sed finibus congue, libero odio ultricies nulla, a pharetra diam mauris non turpis. Phasellus a aliquet lorem.</p>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="http://cdn.makeuseof.com/wp-content/uploads/2012/01/8bit_mushroom_intro.jpg?92a7a3" alt="">
<div class="caption">
<h4>Heading 2</h4>
<p>Nullam pulvinar posuere arcu ac mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis in interdum leo. Nunc sed purus vel eros mollis pharetra sit amet sit amet felis. Ut pharetra mi ac dolor
dignissim facilisis. Phasellus odio massa, pulvinar sed velit ac, malesuada posuere urna. Aliquam urna eros, sollicitudin et diam quis, lacinia feugiat erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Aenean porttitor, tortor sit amet accumsan placerat, ante augue pretium nunc, vel volutpat turpis ex vitae dui. Etiam imperdiet turpis sed ante posuere, venenatis lobortis lorem pharetra.</p>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4 col-xs-12">
<div class="thumbnail">
<img class="img-responsive" src="http://cdn.makeuseof.com/wp-content/uploads/2012/01/8bit_mushroom_intro.jpg?92a7a3" alt="">
<div class="caption">
<h4>Heading 3</h4>
<p>Quisque mollis sollicitudin tellus, in sagittis diam rutrum ut. Mauris ex arcu, vehicula et ullamcorper id, viverra vel nibh. Ut vestibulum tellus vel erat lobortis, quis fringilla nulla semper. Proin a varius tortor, ac varius tortor. Sed fringilla
dignissim neque cursus sit amet. Maecenas molestie hendrerit orci, ut faucibus mauris tempor in.</p>
</div>
</div>
</div>
</div>
&#13;
答案 2 :(得分:1)
我最终使用来自this answer的巧妙技巧,并编写了我自己的代码。 #mq-detector
元素告诉我何时切换媒体查询而不必硬编码视口宽度。然后我迷上了窗口的resize
回调,知道用户何时更改了浏览器窗口的大小:
<script type="text/javascript">
(function() {
$(window).resize(checkAdjustThumbnailHeight);
$(document).ready(checkAdjustThumbnailHeight);
})();
</script>
第一次呈现页面时会调用checkAdjustThumbnailHeight
函数,并且每次浏览器的resize
事件都会触发。该功能检查介质检测器元素并相应地调整缩略图高度:
function checkAdjustThumbnailHeight() {
if ($("#mq-detector > span.visible-lg").is(":visible")) {
adjustThumbnailHeight();
}
else if ($("#mq-detector > span.visible-md").is(":visible")) {
adjustThumbnailHeight();
}
else if ($("#mq-detector > span.visible-sm").is(":visible")) {
inheritThumbailHeight();
}
else if ($("#mq-detector > span.visible-xs").is(":visible")) {
inheritThumbailHeight();
}
else { }
}
请注意,对于sm
和xs
(实际像素值请参阅here)尺寸,我不会修改缩略图高度,因为在这些情况下缩略图会一个在另一个上方呈现,而不是彼此相邻。对于md
和lg
尺寸,我将缩略图高度设置为最大尺寸:
function adjustThumbnailHeight() {
var heights = [ ];
$(".thumbnail").each(function() {
heights.push( $(this).height() );
});
var max = Math.max.apply(null, heights);
$(".thumbnail").each(function() {
$(this).height(max);
});
}
function inheritThumbailHeight() {
$(".thumbnail").each(function() {
$(this).css("height", "inherit");
});
}
这似乎工作正常。
ADDENDUM 由于某些图片可能需要更长时间才能完成加载,因此缩略图的高度可能会偏斜。因此,我使用imageLoaded插件来推迟调整缩略图高度,直到所有图像都完成加载。