简体   繁体   English

将父div高度设置为不溢出的内容

[英]Set parent div height to content that is not overflowing

I have a slider that's in place on my website. 我的网站上有一个滑块。

The basic way that it works is depicted in this jsfiddle - 这个jsfiddle描述了它的基本工作方式-

http://jsfiddle.net/6h7q9/15/ http://jsfiddle.net/6h7q9/15/

I've written code to set the parent's height to the height of the content div. 我编写了将父代的高度设置为内容div高度的代码。 This worked fine, until I introduced some content that did not have a fixed height and whose height might increase while it was being shown on the page. 这很好用,直到我介绍了一些没有固定高度的内容,并且这些高度在页面上显示时可能会增加。 Is there a way, I can dynamically change the height of this parent div whenever content inside it increases or decreases it's height. 有没有办法,只要它内部的内容增加或减少它的高度,我就可以动态地更改它的高度。

HTML - HTML-

<div id="slider_container">
<div id="slider">
    <div id="slide1">
        Has content that might increase the height of the div....
    </div>
    <div id="slide2">
        Has content that might increase the height of the div....
    </div>
    <div id="slide3">
        Has content that might increase the height of the div....
    </div>    
</div>
</div>
<input type="button" value="Next" id="btnNext">
<input type="button" value="Previous" id="btnPrev">
<input type="button" value="Add text" id="btnAddText">
<div class="footer">
    I appear after the largest container, irrespective of which one is present....
</div>

JavaScript - JavaScript-

var currSlider = 1;
$('#btnNext').click(function(){
    debugger;
    var margin = $('#slider').css('margin-left');   
    if(parseInt(margin) <= -400) {
        return;
    }
    currSlider++;
    // Moving the slider
    $('#slider').css('margin-left', parseInt(margin) - 200 + 'px');    
    // Resetting the height...
    $('#slider').height($('#slide' + currSlider).height());
});
$('#btnPrev').click(function(){
    debugger;
    var margin = $('#slider').css('margin-left');
    if(parseInt(margin) >= 0) {
        return;
    }
    currSlider--;
    // Moving to the previous slider
    $('#slider').css('margin-left', parseInt(margin) + 200 + 'px');
    // Resetting the height...
    $('#slider').height($('#slide' + currSlider).height());

});
$('#btnAddText').click(function() {
    $('#slide' + currSlider).text('Hello World'.repeat(100));
});

String.prototype.repeat = function(times) {
   return (new Array(times + 1)).join(this);
};

I hope this "answer" gets you going in the right direction. 我希望这个“答案”能使您朝正确的方向前进。 Since i don't have the time to fully look this up right now, i hope to send you in the right direction. 由于我现在没有时间充分了解此内容,因此我希望向您发送正确的信息。 if you do find out how to do this properly, please shoot me a message, since i would really like to know ^_^ 如果您确实知道如何正确执行此操作,请向我发送一条消息,因为我真的很想知道^ _ ^

An example on how to use: ( DOMSubtreeModified didn't work in IE from what i read. Therefor the propertchange event) 关于如何使用的一个例子:( DOMSubtreeModified没有在IE浏览器从我读为此工作了。 propertchange事件)

$('#slide1').on('DOMSubtreeModified propertychange', function() {
    console.log('test',this);
});

Another option is by using the MutationObserver: 另一种选择是使用MutationObserver:


Updated 6-8-2014 15:00 CET 2014年6月8日更新15:00 CET

Since i totally misread the original post, this answer was useless to say the best. 由于我完全误读了原始帖子,因此最好的答案毫无用处。 But since the problem is actually really easy to solve (or... at least i hope i understand the problem this time), i thought i'd post an answer that worked for the situation: a slider with content of different heights. 但是由于问题实际上很容易解决(或者……至少我希望我这次能理解这个问题),所以我认为我会发布一种适用于这种情况的答案:一个包含不同高度的滑块。

What was the problem with the original slider? 原始滑块有什么问题? You moved the content out of the container, which made it hidden. 您将内容移出了容器,这使其隐藏了。 However, the container would still pick up the height of it, since it only had a fixed width. 但是,由于容器只有固定的宽度,因此它仍将占据容器的高度。 The fixed height on the '#slider' did not prevent the container of picking up the height from the '#slide-*'. “ #slider”上的固定高度不会阻止容器从“#slide- *”中拾取高度。 Had you set the height for the container... all would be fine :-) 如果您设置了容器的高度...就可以了:-)

Here's the outline of the hidden slide, moved 'off canvas': http://i.gyazo.com/f2404e85263a7209907fdbc8f9d8e34e.png 这是隐藏的幻灯片的轮廓,已移出“画布”: http : //i.gyazo.com/f2404e85263a7209907fdbc8f9d8e34e.png

I did not fix your fiddle by completing your code. 我没有通过完成代码来修复您的小提琴。 I just rewrote it to provide you with an easier to maintain slider. 我只是重写了它,以便为您提供易于维护的滑块。 Here's a fiddle with a working slider where you can add and remove stuff in the slides: http://jsfiddle.net/3JL2x/3/ 这是一个带有工作滑块的小提琴,您可以在其中添加和删除幻灯片中的内容: http : //jsfiddle.net/3JL2x/3/

Just remove the height properties in the .slide1, 2 and 3 and add min-height instead. 只需删除.slide1、2和3中的height属性,然后添加min-height Like that : 像那样 :

#slider > div {
    min-height:200px;
    float:left;
    width : 200px;
}
#slide1 {  
    background-color : red;   
}
#slide2 { 
    background-color: green;    
}
#slide3 {    
    background-color: blue; 
}

Live example 现场例子
http://jsfiddle.net/6h7q9/27/ http://jsfiddle.net/6h7q9/27/

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

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