简体   繁体   English

使DOM元素的高度相等

[英]Make DOM elements equal by height

I write JavaScript/TypeScript function: 我编写JavaScript / TypeScript函数:

function setElementsHeightsEqual(el1: HTMLElement, el2: HTMLElement) {
   ...
}

This is typical situation when you want to attach some button (el2) to some input (el1) dynamically. 当您要动态地将某些按钮(el2)附加到某些输入(el1)时,这是典型情况。

I want to make el2's height equal to el1, but I am unable to do it. 我想使el2的高度等于el1,但是我做不到。 There are two problems: 有两个问题:

  1. All possible calls returning height are helpless for me: 所有可能返回高度的呼叫对我来说都是无奈的:

     el1.offsetWidth; // will return 0 if document is not rendered yet el1.style.height; // will return "" if it is set inside external CSS el1.style.paddingTop; // the same el1.style.paddingBottom; // the same 

In other words is there any possibility to calculate height of some specified element? 换句话说,是否有可能计算某些指定元素的高度?

  1. How to set height of el2? 如何设置el2的高度? We do not know even its "box-sizing", the same problem as in (1). 我们甚至都不知道它的“盒子大小”,这与(1)中的问题相同。

Are these tasks impossible in JS + HTML DOM? 这些任务在JS + HTML DOM中是不可能的吗?

offsetHeight is a safe way to go. offsetHeight是一种安全的方法。 If the element is not loaded, move your function call to window.onload. 如果未加载该元素,请将函数调用移至window.onload。

Here you have a working example: 这里有一个工作示例:

 function setMaxHeight(...elements) { console.log(elements); // Get max height var height = 0; for(let element of elements) { console.log(element.offsetHeight); if(element.offsetHeight > height) height = element.offsetHeight; } // Change all elements height to max. for(let element of elements) { element.style.height = height + 'px'; } } window.onload = function() { var object1 = document.querySelector('.object1'); var object2 = document.querySelector('.object2'); var object3 = document.querySelector('.object3'); setMaxHeight(object1, object2, object3); }; 
 .object1 { border: 1px solid #000; display: block; float: left; height: 50px; width: 20%; } .object2 { border: 1px solid #0cc; display: block; float: left; height: 120px; width: 30%; } .object3 { border: 1px solid #afc; display: block; float: left; height: 20px; width: 10%; } 
 <div id="container"> <div class="object1">Text</div> <div class="object2">Text2</div> <div class="object3">Text3</div> </div> 

Compare running with and without the Javascript portion. 比较有无Javascript部分的运行情况。

I hope this helps. 我希望这有帮助。

It is the way I set the equal height to elements I want. 这是我为想要的元素设置相等高度的方法。 Give a class name to your elements which you want to make their height the same. 为您要使其高度相同的元素命名一个类名。

<div class="sameElementHeight">
  <img src="/path/to/Image" class="sameImageHeight">
  <p>YOUR TEXT</p>
</div>
<div class="sameElementHeight">
  <img src="/path/to/Image" class="sameImageHeight">
  <p>YOUR TEXT</p>
</div>
<div class="sameElementHeight">
  <img src="/path/to/Image" class="sameImageHeight">
  <p>YOUR TEXT</p>
</div>

First, create a function like this: 首先,创建如下函数:

equalheight = function(container){

    var currentTallest = 0,
        currentRowStart = 0,
        rowDivs = new Array(),
        $el,
        topPosition = 0;
    jQuery(container).each(function() {

        $el = jQuery(this);
        jQuery($el).height('auto')
        topPostion = $el.position().top;

        if (currentRowStart != topPostion) {
            for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
                rowDivs[currentDiv].height(currentTallest);
            }
            rowDivs.length = 0; // empty the array
            currentRowStart = topPostion;
            currentTallest = $el.height();
            rowDivs.push($el);
        } else {
            rowDivs.push($el);
            currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
        }
        for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
            rowDivs[currentDiv].height(currentTallest);
        }
    });
}

Then call it before closing body tag: 然后在关闭body标签之前调用它:

 <script>
   equalheight(".sameElementHeight");
   equalheight(".sameImageHeight");
 </script>
</body>

You also may call this function when you add new elements to your document. 当您向文档中添加新元素时,也可以调用此函数。 It will calculate the height of the element and set the same height to them and also does the same with images. 它将计算元素的高度并为其设置相同的高度,并且对图像也执行相同的操作。

I hope it helps. 希望对您有所帮助。

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

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