脚本没有在window.resize上运行

时间:2017-06-01 20:25:39

标签: jquery window-resize

以下用于均衡几个div的高度。在页面加载上运行正常,而不是在窗口调整大小。

思想?

function equalHeight(group) {
tallest = 0;
group.each(function() {
   thisHeight = $(this).height();
   if(thisHeight > tallest) {
      tallest = thisHeight;
   }
});
group.height(tallest);
}

$(document).ready(function() {
 equalHeight($(".equal"));
});
$(window).resize(function() {
 equalHeight($(".equal"));
});

2 个答案:

答案 0 :(得分:0)

试试这个

$(function equalHeight(group) {
tallest = 0;
group.each(function() {
   thisHeight = $(this).height();
   if(thisHeight > tallest) {
      tallest = thisHeight;
   }
}));
group.height(tallest);
}

$(document).ready(function() {
 equalHeight($(".equal"));
});
$(window).resize(function() {
 equalHeight($(".equal"));
});

答案 1 :(得分:0)

想出来。这是因为在调整大小时,高度已经从上一次加载函数的运行中设置。我已将$(this).height('auto');添加到each功能,并将$(document).ready(function(){})更改为$(window).load(function(){});请参阅下文:

function equalHeight(group) {
   tallest = 0;
   group.each(function() {
      $(this).height('auto');
      thisHeight = $(this).height();
      if(thisHeight > tallest) {
         tallest = thisHeight;
      }
   });
   group.height(tallest);
}
$(window).load(function() {
   equalHeight($(".equal"));
});
$(window).resize(function() {
   equalHeight($(".equal"));
});