简体   繁体   English

具有不同高度的下拉卷展栏:将高度设置为自动中断下拉列表

[英]Dropdown Rollout with different heights: setting height to auto breaks the rolldown

Right now I have the following fiddle: 现在我有以下小提琴:

body, * {
    margin: 0;
    padding: 0;
}
.wrapper {
    border: 1px solid black;
}

li {
  height: 100px;
}
#two {
    height: 0em;
    overflow: hidden;
    transition-property: height;
    transition: all 1s ease-in-out;
}
.wrapper:hover #two {
    height: 4em;
}

https://jsfiddle.net/ehu2cxe2/ https://jsfiddle.net/ehu2cxe2/

This seems to work, however, in my actual project I set the dropdown menu height by however tall the children are, they all have different heights so that's why I can't give them all a fixed height of, say, 4em. 这似乎有用,但是,在我的实际项目中,我将下拉菜单的高度设置为高,但是孩子们都有,他们都有不同的高度,这就是为什么我不能给他们所有固定高度,比如4em。

I tried to change this to height:auto on hover, but this makes the whole thing break. 我试图将其更改为height:auto悬停,但这会使整个事情破裂。 What am I doing wrong? 我究竟做错了什么?

If you know about how tall the children will be, you can use max-height instead. 如果您知道孩子的身高,您可以使用max-height

 body, * { margin: 0; padding: 0; } .wrapper { border: 1px solid black; } #two { max-height: 0em; overflow: hidden; transition-property: height; transition: all 1s ease-in-out; } .wrapper:hover #two { max-height: 4em; } 
 <div class="wrapper"> <div id="one">(content)</div> <div id="two"> <ul> <li>Menu1</li> <li>Menu2</li> <li>Menu3</li> </ul> </div> </div> 

Or using js/jquery, you can get the rendered list height and apply that as the height to transition 或者使用js / jquery,您可以获得渲染列表高度并将其应用为transition高度

 var h = $('#two ul').outerHeight(), $two = $('#two'), $wrapper = $('.wrapper'); $wrapper.hover(function() { $two.css('height',h); }, function() { $two.css('height','0'); }); 
 body, * { margin: 0; padding: 0; } .wrapper { border: 1px solid black; } #two { height: 0em; overflow: hidden; transition-property: height; transition: all 1s ease-in-out; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div id="one">(content)</div> <div id="two"> <ul> <li>Menu1</li> <li>Menu2</li> <li>Menu3</li> </ul> </div> </div> 

You can also use $.slideToggle() but I prefer keeping the animation/transition in CSS. 你也可以使用$.slideToggle()但我更喜欢在CSS中保持动画/过渡。

 var $twoUl = $('#two ul'), $wrapper = $('.wrapper'); $wrapper.hover(function() { $twoUl.stop().slideToggle(); }); 
 body, * { margin: 0; padding: 0; } .wrapper { border: 1px solid black; } #two { overflow: hidden; } #two ul { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div id="one">(content)</div> <div id="two"> <ul> <li>Menu1</li> <li>Menu2</li> <li>Menu3</li> </ul> </div> </div> 

Using percentages for .wrapper with max-height: 100% , you can skip the javascript and keep it all css . 使用.wrapper百分比为max-height: 100% ,你可以跳过javascript并保持全部css This way you can set your list elements to as many different heights as you need. 这样,您可以根据需要将list元素设置为多个不同的高度。

  body, * { margin: 0; padding: 0; } .wrapper { border: 1px solid black; } #two { max-height: 0em; overflow: hidden; transition-property: height; transition: all 1s ease-in-out; } .wrapper:hover #two { max-height: 100%; } li:nth-child(1) { height: 2.5em; line-height: 2.5em; background-color: lightgrey; } li:nth-child(2) { height: 1.5em; line-height: 1.5em; background-color: grey; } li:nth-child(3) { height: 3.1em; line-height: 3.1em; background-color: dimgrey; } 
 <div class="wrapper"> <div id="one">(content)</div> <div id="two"> <ul> <li>Menu1</li> <li>Menu2</li> <li>Menu3</li> </ul> </div> </div> 

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

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