简体   繁体   English

使列高相同并删除多余的内容

[英]Making the column height same and remove extra content

I have a question, my webpage has many div section and have multiple ul li. 我有一个问题,我的网页上有很多div部分,并且有多个ul li。 It is 2 column design and what i need to do is to set the same height for both corresponding div section. 这是2列设计,我需要为两个相应的div部分设置相同的高度。 For example check the image, it is one section and what i want is to set the height for the shortest div and remove the large div content. 例如检查图像,这是一个部分,我想要的是设置最短div的高度并删除大div的内容。 Is this possible to do? 这可能吗?

I checked on internet but what i am getting is to set the both column height but unable to find how can i remove the content. 我在互联网上检查了一下,但是得到的却是设置两个列的高度,但找不到如何删除内容。

The right image is what i want. 正确的图像是我想要的。

Please help :) 请帮忙 :)

在此处输入图片说明

Assuming you're using jQuery, try this: 假设您使用的是jQuery,请尝试以下操作:

var $targets = $('.awesomeClass'),
    targetHeight = $targets.first().height();

$targets.each(function() {
    var $this = $(this);
    if( $this.height() < targetHeight ) {
        targetHeight = $this.height();
    }
});

$targets.height( targetHeight );

This code will set the height of all matching elements to the height of the shortest member. 此代码会将所有匹配元素的高度设置为最短成员的高度。

Just make sure all your target elements have the class awesomeClass , or whatever class you decide to use, and add an overflow: hidden; 只需确保所有目标元素都具有awesomeClass类或您决定使用的任何类,然后添加一个overflow: hidden; so the overflowing elements are cut off. 因此,溢出元素被切断。 This method will remain functional even if you decide to change the number of elements etc. 即使您决定更改元素数量等,此方法也将保持功能。

Say you have following two div 's on the page as: 假设您在页面上遵循以下两个div

<div class="left">
<ul>/<ul>
</div>

<div class="right">
<ul></ul>
</div>

You want both of them to have the same height ie the minimum out of the two. 您希望它们都具有相同的高度,即两者中的最小值。 Well, its simple with following jQuery Code: 好吧,下面的jQuery代码很简单:

var leftHeight = $(".left").height();
var rightHeight = $(".right").height();

if(leftHeight > rightHeight)
{
$(".left").height(rightHeight);
}
else
{
$(".right").height(leftHeight);
}

Of course you need to set Overflow-y:hidden, for the div and ul , so the scroll bar doesn't appear. 当然,您需要为divul设置Overflow-y:hidden,这样滚动条就不会出现。

It's better to set explicit height, then to remove html elements 最好设置明确的高度,然后删除html元素

Removes elements until the first div height is less than or equal to the second one: 删除元素,直到第一个div高度小于或等于第二个:

while($('#div1').height() > $('#div2').height()) {
    $('#div1').find('li:last-child').remove();
}

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

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