简体   繁体   English

找出div高度和设置div高度

[英]Find out divs height and setting div height

I'm quite new to javascript/jquery stuff, but i would like to do this kind of thing with it: 我对javascript / jquery的东西很新,但我想用它做这样的事情:

I have four divs side-by-side like this and content in each one. 我有四个这样的div并排在每个div中。 Now if one has more content it gets stretched higher. 现在,如果一个人有更多的内容,它会变得更高。 I would like to make the other divs as high too even if they don't have as much content. 即使他们没有那么多内容,我也希望其他div也高。 So basically i want the script goes through the divs and check the heights and sets all of divs heights to same as the highest div has. 所以基本上我希望脚本通过div并检查高度并将所有div高度设置为与最高div相同。 Hopefully you understand :) 希望你明白:)

I was getting a NaN error with SLaks code. 我在SLaks代码中遇到NaN错误。 Giving maxHeight an initial value of 0 did the trick. 给maxHeight一个初始值为0就可以了。

var maxHeight = 0;
$('div')
  .each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
  .height(maxHeight);

Thanks SLaks! 谢谢SLaks!

You can use jQuery's height method to get and set the height of an element. 您可以使用jQuery的height方法来获取和设置元素的高度。

You need to loop through the elements, find the tallest one, then set the height of all of the elements. 您需要遍历元素,找到最高的元素,然后设置所有元素的高度。

var maxHeight;
$('div')
    .each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
    .height(maxHeigt);

Replace 'div' with a jQuery selector that selects the elements you want to equalize. 'div'替换为选择要均衡的元素的jQuery选择器。

<script>
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
$(document).ready(function() {
    equalHeight($(".column"));
});
</script>

see this example 看这个例子

This is straight out of the jQuery documentation pages: 这直接来自jQuery文档页面:

$.fn.equalizeHeights = function(){
  return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
}
$('input').click(function(){
  $('div').equalizeHeights();
});

This is simple code 这是简单的代码

var heights = $("element").map(function ()
{
    return $(this).height();
}).get(),

MaxHeight = Math.max.apply(null, heights);

or 要么

var highest = null;
var hi = 0;
$(".testdiv").each(function(){
var h = $(this).height();
if(h > hi){
   hi = h;
 highest = $(this);  
 }    
});

highest.css("background-color", "red");

我相信你正在寻找这个插件:Benize Alman的equalizeBottoms

Well you can get and set the height with height() 那么你可以得到并设置height()

height = $('#id').height()

Loop through the elements and check the height, eg: 循环遍历元素并检查高度,例如:

$elements = $('.container');

var max_height = 0;

for ($element in $elements) {
    if (max_height > $element.height()) max_height = $element.height();
}

$elements.height(max_height);

Wow - people really want to force jQuery trickery into this one -- you can do it all with CSS. 哇 - 人们真的想把jQuery技巧强加到这个 - 你可以用CSS做到这一切。 From stopdesign : 来自stopdesign

When creating liquid layouts using CSS, here are a few considerations I keep in mind: 使用CSS创建液体布局时,请记住以下几点:

  • Double-div columns: As much as I hate to use extra markup, there's no easier way to ensure maximum compatibility across multiple browsers than to use two divs for each column. Double-div专栏:尽管我讨厌使用额外的标记,但是没有更简单的方法来确保跨多个浏览器的最大兼容性,而不是为每列使用两个div。 The outer divs are used for setting widths. 外部div用于设置宽度。 Inner divs are used to set padding to create gutters of white space between each column. 内部div用于设置填充以在每列之间创建白色空间的沟槽。

  • Use fixed-width gutters (or gutter widths based on type size): When column width gets applied independently of column padding, as above, percentages can be used for widths, and pixels or ems can be used for padding. 使用固定宽度的檐槽(或基于类型大小的檐槽宽度):当列宽度独立于列填充应用时,如上所述,百分比可用于宽度,像素或ems可用于填充。 This, without worrying about one column getting bumped to the bottom of another, or without purposely under-sizing columns so they always fit on the page. 这一点,不用担心一列撞到另一列的底部,或者没有故意调整列的大小,因此它们总是适合页面。 Even though column widths change as the browser gets wider or narrower, the text on the page generally stays the same size. 尽管随着浏览器变宽或变窄,列宽会发生变化,但页面上的文本通常保持相同的大小。 The amount of white space required to make the text feel comfortable is dependent more on the size of the type, rather than the size of the column containing that text. 使文本感觉舒适所需的空白量更多地取决于类型的大小,而不是包含该文本的列的大小。

  • Avoid fixed-width columns: As much as possible, make all columns a percentage width. 避免使用固定宽度的列:尽可能使所有列的宽度均为百分比。 It's tempting to think of sidebars and navigation columns as being one static width, and let the main or middle column do all the expanding. 将侧边栏和导航列视为一个静态宽度很容易,让主列或中间列进行所有扩展。 This quickly destroys any proportions that may have been carefully chosen, as the fixed width columns can look puny and weak at large resolutions. 这会快速破坏可能经过精心挑选的任何比例,因为固定宽度的列在大分辨率下看起来微不足道。 Or wide fixed-width sidebars can become daunting, over-powering the main column at narrower browser widths. 或者宽的固定宽度的侧边栏可能会变得令人生畏,在较窄的浏览器宽度下过度供电主柱。

The trick is to create an ultra-wide background image (or two images for 3-column layouts, thus the Sliding-Doors-nature of the technique). 诀窍是创建一个超宽的背景图像(或三个列布局的两个图像,因此该技术的滑动门性质)。 The image is partially opaque, and partially transparent. 图像部分不透明,部分透明。 It gets tiled vertically inside the parent container, just like Dan's Faux Columns technique. 它在父容器内垂直平铺,就像Dan的Faux Columns技术一样。 The opaque portion of the image should match percentages of the column it helps create, so that it can be positioned with an identical background-position value. 图像的不透明部分应与其帮助创建的列的百分比相匹配,以便可以使用相同的背景位置值进行定位。 This allows the transition from opaque to transparent to become the axis point for the background's position. 这允许从不透明到透明的过渡成为背景位置的轴点。 The opaque portion's position within the image could be altered based on order of content within the HTML to produce almost any effect desired. 可以基于HTML内的内容顺序改变图像内的不透明部分的位置,以产生几乎任何所需的效果。

Of course, if all you want is to fix the equal-height issue, what you want is the One True Layout: 当然,如果您想要的只是修复等高问题,那么您需要的是One True Layout:

http://fu2k.org/alex/css/onetruelayout/example/interactive http://fu2k.org/alex/css/onetruelayout/example/interactive

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

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