简体   繁体   English

将div调整为自动大小,然后在调整窗口大小时将其高度设置为彼此相同?

[英]resize divs to auto and then to same height as one another upon window resize?

I have three column divs I would like to resize so that they are all the same lenghth. 我要调整三个列的div的大小,以使它们的长度相同。 I have been able to do this upon page load by executing the following function (which I found on SO though unfortunately I am unable to find it now to give attribution, will update if I can find it): 通过执行以下功能,我能够在页面加载时执行此操作(很遗憾,我在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"});
}

This works fine, but I also want the divs to resize each time the window resizes. 这工作正常,但我还希望每次窗口调整大小时都重新调整div的大小。 So I modified the function and then call it each time the window resizes and call the function then as well as upon page load. 因此,我修改了该函数,然后在每次窗口调整大小时调用它,然后在页面加载时调用该函数。 Here is the modified function (first line of function is the addition) plus the function call for a window resize: 这是修改后的函数(函数的第一行是添加项)以及用于窗口大小调整的函数调用:

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

Now my div's do not resize properly upon page load or upon page resize. 现在,我的div在页面加载或页面调整大小时无法正确调整大小。 Nothing happens at all. 什么都没发生。 If I put some kind of alert in my function, it is not called when the 如果我在函数中添加了某种警报,则当

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

line is included in the function. 行包含在函数中。 Why is this line breaking my code? 为什么这行会破坏我的代码? The object does exist when I call it on the first line, so it's not a null object. 当我在第一行调用该对象时,该对象确实存在,因此它不是空对象。

Why not try resizing with media queries? 为什么不尝试通过媒体查询调整大小? This will allow the divs to resize upon window resizing without need for page reload 这将允许div在调整窗口大小时调整大小,而无需重新加载页面

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM