window.resize 上的脚本不是 运行

Script not running on window.resize

下面是用来均衡几个div的高度的。在页面加载时运行良好,在 window 调整大小时不工作。

想法?

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"));
});

试试这个

$(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"));
});

想通了。这是因为在调整大小时,高度已经从加载函数的前一个 运行 设置。我已将 $(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"));
});