【问题标题】:resize divs to auto and then to same height as one another upon window resize?在窗口调整大小时将 div 的大小调整为自动,然后调整为彼此相同的高度?
【发布时间】:2015-03-17 05:05:20
【问题描述】:

我想调整三列 div 的大小,使它们的长度都相同。我已经能够通过执行以下函数在页面加载时做到这一点(我在 SO 上找到了它,但不幸的是我现在无法找到它来给出归属,如果我能找到它会更新):

function resizeIt()
{   
var largest = 0;

$(".feature").each(function(){ //loop through each section
   var findHeight = $(this).height(); //find the height

   if(findHeight > largest){ //see if this height is greater than "largest" height
      largest = findHeight; //if it is greater, set largest height to this one
   }
});

$(".feature").css({"height":largest+"px"});
}

这很好用,但我也希望每次调整窗口大小时都调整 div 的大小。所以我修改了该函数,然后在每次窗口调整大小时调用它,然后在页面加载时调用该函数。这是修改后的函数(第一行函数是加法)加上调整窗口大小的函数调用:

function resizeIt()
{
$(".feature").css({"height: auto"});

var largest = 0;

$(".feature").each(function(){ //loop through each section
   var findHeight = $(this).height(); //find the height

   if(findHeight > largest){ //see if this height is greater than "largest" height
      largest = findHeight; //if it is greater, set largest height to this one
   }
});

$(".feature").css({"height":largest+"px"});
}
resizeIt();   

$(window).resize(function(){
resizeIt();
});

现在我的 div 在页面加载或页面调整大小时无法正确调整大小。什么都没有发生。如果我在我的函数中加入某种警报,当

$(".feature").css({"height: auto"});

line 包含在函数中。为什么这一行会破坏我的代码?当我在第一行调用它时,该对象确实存在,因此它不是空对象。

【问题讨论】:

  • 应该是$(".feature").css({"height": "auto"});
  • @LShetty 就是这样。谢谢。

标签: javascript jquery html css resize


【解决方案1】:

为什么不尝试使用媒体查询调整大小?这将允许 div 在调整窗口大小时调整大小,而无需重新加载页面

@media (max-width: 1200px) {
.feature {
/*new height value*/
/*new width value*/
}
}
@media (max-width: 767px) {
.feature {
/*new height value*/
/*new width value*/
}
}

【讨论】:

  • 感谢您的提示,但这并不是我想要的。这些 div 中的内容会不时变化,所以我宁愿根本不设置特定的高度。
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多