简体   繁体   English

内部DIV展开/折叠后,周围DIV的设置高度

[英]Setting height of surrounding DIV after inner DIV is expanded/collapsed

I have a simple accordion. 我有一个简单的手风琴。 What i am trying to do is that when an item within the content is clicked (How to reach us) for example, the hidden subsequent text is expanded. 我想做的是,例如,当单击内容中的某个项目(如何联系我们)时,隐藏的后续文本将被扩展。 Once expanded, the new height of the content DIV is calculated. 展开后,将计算content DIV的新高度。 This newly calculated content DIV height is then assigned to the height of .wrapper . 然后将此新计算的content DIV高度分配给.wrapper的高度。 The red wrapper should then surround the entire box. 然后,红色包装纸应包围整个包装盒。

See fiddle: http://jsfiddle.net/oampz/jLQf4/1/ 请参阅小提琴: http : //jsfiddle.net/oampz/jLQf4/1/

HTML: HTML:

<div class="wrapper">
    <ul class="panel">
        <li>
            <div class="heading">   <a>Menu 1</a>

            </div>
            <div class="content">
                <ul>
                    <h2>Top Ten Questions</h2>
                    <li>    
                        <a href="">How to reach us</a>
                        <p class="hide">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
                    </li>
                    <li>
                        <a href="">How to email us</a>
                        <p class="hide">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
                    </li>
                    <li>
                        <a href="">Contact Number</a>
                        <p class="hide">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
                    </li>
                </ul>
            </div>
        </li>
    </ul>
</div>

CSS: CSS:

.heading {
    color: white;
    font-weight: bold;
    padding: 15px 0 15px 20px;
    background: grey;
}
.content {
    background: #99CCFF;
    position: absolute;
}
.hide {
    display: none;
}

.wrapper {
    background: #FFD1E0;
    position: relative;
}

jQuery: jQuery的:

$(".content ul li a").click(function () {
  var $this = $(this);
  $this.next(".content ul li a").show(400);
  $this.parent().siblings().children().next().hide();
  return false;
});

UPDATE UPDATE ** * ** * ** * ** * ** * ** * ** * ** * ** * **** ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

After the .onClick, would something like: .onClick之后,将是:

$(".wrappper").css({'height':($(".content").height()+'px')});

Work? 工作?

UPDATE UPDATE ** * ** * ** * ** * ** * ** * ** * ** * ** * **** ** * ** * ** * ** * ** * ** * ** * ** * ** * ****

Updated fiddle: http://jsfiddle.net/oampz/jLQf4/9/ 更新的小提琴: http : //jsfiddle.net/oampz/jLQf4/9/

If I understand your issue correctly, then this may help: 如果我正确理解了您的问题,那么这可能会有所帮助:

Demo Fiddle 演示小提琴

Your content div doesn't have a specified width, so it's automatically wrapping whatever is inside of it, when you show your text it extends out further than it was initially. 您的content div没有指定的宽度,因此它会自动换行其中的任何内容,当您显示文本时,它会比最初延伸得更远。

EDIT - I think that there may be an easier way to go about what you're trying to accomplish. 编辑 -我认为可能会有更简单的方法来解决您要完成的任务。 Instead of using JS to dynamically update the .wrapper I think it would be easier to remove the position: absolute from .content . 我认为,与其使用JS动态更新.wrapper.content删除position: absolute会更容易。 Some padding seemed to make it look the same. 一些填充似乎使它看起来相同。

CSS: CSS:

.content {
    width: 280px;
    background: #99CCFF;
    padding: 10px 0;

    //position:absolute;
}

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

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