【发布时间】:2015-03-23 08:52:37
【问题描述】:
我正在尝试根据每行的最高高度 (http://tabletlisting.com/) 调整我的 Material Design 卡片的大小。这是我在全局助手中使用的。
Template.registerHelper('setCardHeight',function(tabletCards){
var tabletCount = tabletCards.length;
var tabletCardWidth = 450;
var screenWidth = $(window).width();
var cardsPerRow = Math.floor(screenWidth / tabletCardWidth);
if (cardsPerRow > 1) {
for (var i=0; i<tabletCount; i+=cardsPerRow) {
var maxHeight = $(tabletCards[i]).height();
for (var j=0; j<cardsPerRow; j++) {
if (j+i < tabletCount) {
currentCardHeight = $(tabletCards[j+i]).height();
if (currentCardHeight > maxHeight) {
maxHeight = currentCardHeight;
}
}
}
maxHeight = maxHeight + 10;
var setHeight = 'height: ' + maxHeight + 'px';
for (var j=0; j<cardsPerRow; j++) {
if (j+i < tabletCount) {
tabletCards[j+i].setAttribute('style', setHeight);
}
}
}
}
});
这可行,但每次站点菜单上的过滤器之一导致视图更改时,我都需要调用此函数。我在 Template.mytemplate.onRendered 中尝试过这个,但它只在初始加载时调用它。
Template.tabletsList.onRendered(function() {
var allTabletCards = this.findAll('.tablet-card')
UI._globalHelpers.setCardHeight(allTabletCards);
});
在 DOM 加载后,我应该使用什么来在每次渲染更改时使用此函数回调。谢谢。
【问题讨论】:
-
为了提高代码的易读性,更喜欢这个gist.github.com/Rebolon/fadaf1c67799fb6bc54d(不是你的问题的解决方案;-)
标签: meteor meteor-blaze