Fork me on GitHub

在响应式布局下,保证不同元素的相同高度

在响应式布局中限制不同元素的高度依靠单纯的css恐怕是无能为力了,但我们可以通过jQuery中的matchHeight.js可以将全部所选元素等高。

再有就是将处在同一行的元素等高。原理就是在在同一行元素中选择最高的,然后将所有元素的高度设置成该数值,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
equalheight = function(container){
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$(container).each(function() {
$el = $(this);
$($el).height('auto')
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.height();
rowDivs.push($el);
} else {
rowDivs.push($el);
currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
}
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
});
}
//eg比如想具有相同的class的元素有相等的高度
$(window).load(function() {
equalheight('.eqheight');
});
$(window).resize(function(){
equalheight('.eqheight');
});
据说帅的人都赏给博主几块钱零花钱。