我正在尝试使用CSS过渡使<ul>滑落。

<ul>height: 0; 。 悬停时,将高度设置为height:auto; 。 但是,这导致它只是显示而不是过渡,

如果我从height: 40px; 达到height: auto; ,那么它将向上滑动到height: 0; ,然后突然跳到正确的高度。

不使用JavaScript,我还能怎么做?

 #child0 { height: 0; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease; } #parent0:hover #child0 { height: auto; } #child40 { height: 40px; overflow: hidden; background-color: #dedede; -moz-transition: height 1s ease; -webkit-transition: height 1s ease; -o-transition: height 1s ease; transition: height 1s ease; } #parent40:hover #child40 { height: auto; } h1 { font-weight: bold; } 
 The only difference between the two snippets of CSS is one has height: 0, the other height: 40. <hr> <div id="parent0"> <h1>Hover me (height: 0)</h1> <div id="child0">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div> </div> <hr> <div id="parent40"> <h1>Hover me (height: 40)</h1> <div id="child40">Some content <br>Some content <br>Some content <br>Some content <br>Some content <br>Some content <br> </div> </div> 

#1楼

我没有详细阅读所有内容,但是最近遇到了这个问题,我做了以下工作:

div.class{min-height:1%;max-height:200px;-webkit-transition: all 0.5s ease;-moz-transition: all 0.5s ease;-o-transition: all 0.5s ease;-webkit-transition: all 0.5s ease;transition: all 0.5s ease;overflow:hidden;
}div.class:hover{min-height:100%;max-height:3000px;
}

这样一来,您可以创建一个div,该div首先显示最高200像素的高度,然后将其悬停,其大小至少应与div的整个内容一样高。 Div不会变为3000px,但我要施加的极限是3000px。 确保在non:hover上进行过渡,否则可能会得到一些奇怪的渲染。 这样,:hover将从非:hover继承。

从px到%或自动的过渡均无效。 您需要使用相同的度量单位。 这对我来说很好。 使用HTML5使其完美。

记住,总有一个解决方案……; )

希望有人觉得这有用


#2楼

您应该改用scaleY。

HTML:

<p>Here (scaleY(1))</p>
<ul><li>Coffee</li><li>Tea</li><li>Milk</li>
</ul>

CSS:

ul {background-color: #eee;transform: scaleY(0);    transform-origin: top;transition: transform 0.26s ease;
}p:hover ~ ul {transform: scaleY(1);
}

我已经在jsfiddle上为上述代码制作了供应商前缀版本的http://jsfiddle.net/dotnetCarpenter/PhyQc/9/,并更改了jsfiddle以使用scaleY而不是高度, http://jsfiddle.net/dotnetCarpenter/ 7cnfc / 206 / 。


#3楼

扩展@jake的答案,过渡将一直达到最大高度值,从而产生非常快的动画-如果同时为:hover和off设置过渡,则可以进一步控制疯狂速度。

所以li:hover是鼠标进入状态时的状态,然后非悬停属性上的过渡将是鼠标离开。

希望这会有所帮助。

例如:

.sidemenu li ul {max-height: 0px;-webkit-transition: all .3s ease;-moz-transition: all .3s ease;-o-transition: all .3s ease;-ms-transition: all .3s ease;transition: all .3s ease;
}
.sidemenu li:hover ul {max-height: 500px;-webkit-transition: all 1s ease;-moz-transition: all 1s ease;-o-transition: all 1s ease;-ms-transition: all 1s ease;transition: all 1s ease;
}
/* Adjust speeds to the possible height of the list */

这是一个小提琴: http : //jsfiddle.net/BukwJ/


#4楼

杰克(Jake)为最大高度设置动画的答案很棒,但是我发现设置较大的最大高度令人讨厌而导致延迟。

可以将可折叠内容移动到一个内部div中,并通过获取内部div的高度来计算最大高度(通过JQuery,它将是externalHeight())。

$('button').bind('click', function(e) { e.preventDefault();w = $('#outer');if (w.hasClass('collapsed')) {w.css({ "max-height": $('#inner').outerHeight() + 'px' });} else {w.css({ "max-height": "0px" });}w.toggleClass('collapsed');
});

这是一个jsfiddle链接: http : //jsfiddle.net/pbatey/duZpT

这是一个jsfiddle,所需的代码绝对最少: http : //jsfiddle.net/8ncjjxh8/


#5楼

编辑:向下滚动以获取更新的答案
我正在制作一个下拉列表,并看到了此帖子……许多不同的答案,但我决定也分享我的下拉列表,……虽然不完美,但至少它将仅使用CSS进行下拉! 我一直在使用transform:translateY(y)将列表转换为视图...
您可以在测试中看到更多
http://jsfiddle.net/BVEpc/4/
我将div放在每个li后面,因为我的下拉列表来自上层,为了正确显示它们是必需的,我的div代码是:

#menu div {transition: 0.5s 1s;z-index:-1;-webkit-transform:translateY(-100%);-webkit-transform-origin: top;
}

悬停是:

#menu > li:hover div {transition: 0.5s;-webkit-transform:translateY(0);
}

并且因为ul height设置为内容,所以它可以覆盖您的身体内容,这就是我为ul执行此操作的原因:

 #menu ul {transition: 0s 1.5s;visibility:hidden;overflow:hidden;
}

并悬停:

#menu > li:hover ul {transition:none;visibility:visible;
}

过渡后的第二次是延迟,在我的下拉列表被动画关闭后,它将隐藏起来...
希望以后有人从中受益。

编辑:我简直不敢相信ppl实际上使用了这个原型! 该下拉菜单仅适用于一个子菜单,仅此而已!! 我已经更新了一个更好的版本,可以在支持IE 8的同时为ltr和rtl方向提供两个子菜单。
LTR小提琴
RTL小提琴
希望将来有人会有用。


#6楼

这是一种从任何起始高度(包括0)过渡到自动(全尺寸且灵活)的方法,而无需在每个节点上进行硬设置代码或初始化任何用户代码: https : //github.com/csuwildcat / transition-auto 。 我相信这基本上是您想要的圣杯-> http://codepen.io/csuwldcat/pen/kwsdF 。 只需将下面的JS文件拍到您的页面中,然后要做的就是从要扩展和收缩的节点中添加/删除一个布尔值属性-“ reveal=""

一旦包含了示例代码下面的代码块,您需要做的就是这一切:

/*** Nothing out of the ordinary in your styles ***/
<style>div {height: 0;overflow: hidden;transition: height 1s;}
</style>/*** Just add and remove one attribute and transition to/from auto! ***/<div>I have tons of content and I am 0px in height you can't see me...
</div><div reveal>I have tons of content and I am 0px in height you can't see me...but now that you added the 'reveal' attribute, I magically transitioned to full height!...
</div>

这是要包含在您的页面中的代码块,在此之后,所有内容都是肉汁:

将此JS文件放入您的页面中-Just Just™全部使用

/ *高度代码:自动; 过渡* /

(function(doc){/* feature detection for browsers that report different values for scrollHeight when an element's overflow is hidden vs visible (Firefox, IE) */
var test = doc.documentElement.appendChild(doc.createElement('x-reveal-test'));test.innerHTML = '-';test.style.cssText = 'display: block !important; height: 0px !important; padding: 0px !important; font-size: 0px !important; border-width: 0px !important; line-height: 1px !important; overflow: hidden !important;';
var scroll = test.scrollHeight || 2;
doc.documentElement.removeChild(test);var loading = true,numReg = /^([0-9]*\.?[0-9]*)(.*)/,skipFrame = function(fn){requestAnimationFrame(function(){requestAnimationFrame(fn);});},/* 2 out of 3 uses of this function are purely to work around Chrome's catastrophically busted implementation of auto value CSS transitioning */revealFrame = function(el, state, height){el.setAttribute('reveal-transition', 'frame');el.style.height = height;skipFrame(function(){el.setAttribute('reveal-transition', state);el.style.height = '';});},transitionend = function(e){var node = e.target;if (node.hasAttribute('reveal')) {if (node.getAttribute('reveal-transition') == 'running') revealFrame(node, 'complete', '');} else {node.removeAttribute('reveal-transition');node.style.height = '';}},animationstart = function(e){var node = e.target,name = e.animationName;   if (name == 'reveal' || name == 'unreveal') {if (loading) return revealFrame(node, 'complete', 'auto');var style = getComputedStyle(node),offset = (Number(style.paddingTop.match(numReg)[1])) +(Number(style.paddingBottom.match(numReg)[1])) +(Number(style.borderTopWidth.match(numReg)[1])) +(Number(style.borderBottomWidth.match(numReg)[1]));if (name == 'reveal'){node.setAttribute('reveal-transition', 'running');node.style.height = node.scrollHeight - (offset / scroll) + 'px';}else {if (node.getAttribute('reveal-transition') == 'running') node.style.height = '';else revealFrame(node, 'running', node.scrollHeight - offset + 'px');}}};doc.addEventListener('animationstart', animationstart, false);
doc.addEventListener('MSAnimationStart', animationstart, false);
doc.addEventListener('webkitAnimationStart', animationstart, false);
doc.addEventListener('transitionend', transitionend, false);
doc.addEventListener('MSTransitionEnd', transitionend, false);
doc.addEventListener('webkitTransitionEnd', transitionend, false);/*Batshit readyState/DOMContentLoaded code to dance around Webkit/Chrome animation auto-run weirdness on initial page load.If they fixed their code, you could just check for if(doc.readyState != 'complete') in animationstart's if(loading) check
*/
if (document.readyState == 'complete') {skipFrame(function(){loading = false;});
}
else document.addEventListener('DOMContentLoaded', function(e){skipFrame(function(){loading = false;});
}, false);/* Styles that allow for 'reveal' attribute triggers */
var styles = doc.createElement('style'),t = 'transition: none; ',au = 'animation: reveal 0.001s; ',ar = 'animation: unreveal 0.001s; ',clip = ' { from { opacity: 0; } to { opacity: 1; } }',r = 'keyframes reveal' + clip,u = 'keyframes unreveal' + clip;styles.textContent = '[reveal] { -ms-'+ au + '-webkit-'+ au +'-moz-'+ au + au +'}' +'[reveal-transition="frame"] { -ms-' + t + '-webkit-' + t + '-moz-' + t + t + 'height: auto; }' +'[reveal-transition="complete"] { height: auto; }' +'[reveal-transition]:not([reveal]) { -webkit-'+ ar +'-moz-'+ ar + ar +'}' +'@-ms-' + r + '@-webkit-' + r + '@-moz-' + r + r +'@-ms-' + u +'@-webkit-' + u + '@-moz-' + u + u;doc.querySelector('head').appendChild(styles);})(document);

/ *演示代码* /

    document.addEventListener('click', function(e){if (e.target.nodeName == 'BUTTON') {var next = e.target.nextElementSibling;next.hasAttribute('reveal') ? next.removeAttribute('reveal') : next.setAttribute('reveal', '');}}, false);

#7楼

我能够做到这一点。 我有一个.child.parent div。 孩子的div完全符合母公司的宽度/高度与内absolute定位。 然后,我为translate属性设置动画,以将其Y值降低100% 。 它的动画非常流畅,没有毛刺或缺点,就像这里的任何其他解决方案一样。

像这样的伪代码

.parent{ position:relative; overflow:hidden; }
/** shown state */
.child {position:absolute;top:0;:left:0;right:0;bottom:0;height: 100%;transition: transform @overlay-animation-duration ease-in-out;.translate(0, 0);
}/** Animate to hidden by sliding down: */
.child.slidedown {.translate(0, 100%); /** Translate the element "out" the bottom of it's .scene container "mask" so its hidden */
}

您可以在.parent上指定一个height ,以px%或保留为auto 。 然后,当该div向下滑动时,该div会掩盖.child div。


#8楼

我最近一直在将li元素的max-height而不是包装ul过渡。

原因是,与较大的max-heights相比,较小的max-heights的延迟远不那么明显(如果有的话),我还可以相对于lifont-size设置max-height值,而不是任意设置使用emsrems数量巨大。

如果我的字体大小是1rem ,则将max-height设置为3rem (以容纳3rem文本)。 您可以在此处查看示例:

http://codepen.io/mindfullsilence/pen/DtzjE


#9楼

如果提供的硬编码最大高度值不大于实际高度,则杰克(Jake)的最大高度解决方案就可以很好地工作(因为否则会有不希望的延迟和计时问题)。 另一方面,如果硬编码值偶然不大于实际高度,则该元素将不会完全打开。

以下仅CSS解决方案还需要硬编码的大小,该大小应大于大多数实际大小。 但是,如果实际大小在某些情况下大于硬编码大小,则此解决方案也适用。 在那种情况下,过渡可能会跳一点,但绝不会留下部分可见的元素。 因此,该解决方案也可以用于未知内容,例如来自数据库的未知内容,您只知道其中的内容通常不大于x像素,但是也有例外。

想法是对底边距使用负值(对于稍有不同的动画,对边距顶部使用负值),然后将内容元素放入带有overflow:hidden的中间元素中。 内容元素的负边距会降低中间元素的高度。

下面的代码使用从-150px到0px的margin-bottom过渡。 只要content元素不高于150px,这单独就可以正常工作。 此外,它对中间元素的最大高度使用了从0px到100%的过渡。 如果content元素大于150px,则最终将隐藏中间元素。 对于最大高度,过渡仅用于在关闭时将其应用延迟一秒,而不是获得平滑的视觉效果(因此过渡范围可以从0px到100%)。

CSS:

.content {transition: margin-bottom 1s ease-in;margin-bottom: -150px;
}
.outer:hover .middle .content {transition: margin-bottom 1s ease-out;margin-bottom: 0px
}
.middle {overflow: hidden;transition: max-height .1s ease 1s;max-height: 0px
}
.outer:hover .middle {transition: max-height .1s ease 0s;max-height: 100%
}

HTML:

<div class="outer"><div class="middle"><div class="content">Sample Text<br> Sample Text<br> Sample Text<div style="height:150px">Sample Test of height 150px</div>Sample Text</div></div>Hover Here
</div>

边距下限值应为负,并尽可能接近内容元素的实际高度。 如果它的(绝对值)较大,则存在与最大高度解决方案相似的延迟和时序问题,但是,只要硬编码的大小不比实际大小大很多,就可以限制它们。 如果边缘下限的绝对值小于实际高度,则过渡会略微跳跃。 在过渡之后的任何情况下,内容元素都将完全显示或完全删除。

有关更多详细信息,请参阅我的博客文章http://www.taccgl.org/blog/css_transition_display.html#combined_height


#10楼

可接受的答案在大多数情况下都有效,但是当div高度变化很大时,效果并不理想-动画速度不取决于内容的实际高度,并且看起来可能很不稳定。

您仍然可以使用CSS来执行实际的动画,但是您需要使用JavaScript来计算项目的高度,而不是尝试使用auto 。 不需要jQuery,但是如果要兼容,则可能需要稍作修改(在最新版本的Chrome中可用:)。

 window.toggleExpand = function(element) { if (!element.style.height || element.style.height == '0px') { element.style.height = Array.prototype.reduce.call(element.childNodes, function(p, c) {return p + (c.offsetHeight || 0);}, 0) + 'px'; } else { element.style.height = '0px'; } } 
 #menu #list { height: 0px; transition: height 0.3s ease; background: #d5d5d5; overflow: hidden; } 
 <div id="menu"> <input value="Toggle list" type="button" onclick="toggleExpand(document.getElementById('list'));"> <ul id="list"> <!-- Works well with dynamically-sized content. --> <li>item</li> <li><div style="height: 100px; width: 100px; background: red;"></div></li> <li>item</li> <li>item</li> <li>item</li> </ul> </div> 

#11楼

对每个状态使用具有不同过渡缓和和延迟的max-height

HTML:

<a href="#" id="trigger">Hover</a>
<ul id="toggled"><li>One</li><li>Two</li><li>Three</li>
<ul>

CSS:

#toggled{max-height: 0px;transition: max-height .8s cubic-bezier(0, 1, 0, 1) -.1s;
}#trigger:hover + #toggled{max-height: 9999px;transition-timing-function: cubic-bezier(0.5, 0, 1, 0); transition-delay: 0s;
}

参见示例: http : //jsfiddle.net/0hnjehjc/1/


#12楼

我想我提出了一个非常可靠的解决方案

好! 我知道这个问题与互联网一样古老,但是我认为我有一个解决方案,后来我将其变成了名为mutant-transition的插件 。 每当DOM发生更改时,我的解决方案都会为跟踪元素设置style=""属性。 最终结果是,您可以在转换中使用优质的ole CSS,而无需使用hacky修复程序或特殊的javascript。 唯一要做的就是使用data-mutant-attributes="X"设置要在相关元素上跟踪的内容。

<div data-mutant-attributes="height">                                                                      This is an example with mutant-transition                                                                                                          </div>

而已! 该解决方案使用MutationObserver来跟踪DOM中的更改。 因此,您实际上不需要设置任何内容或使用javascript手动设置事物的动画。 更改会自动跟踪。 但是,由于它使用MutationObserver,因此只能在IE11 +中转换。

小提琴!

  • 演示从height: autoheight: 100%过渡
  • 演示过渡height: auto添加子项时height: auto

#13楼

我一直使用的解决方案是先淡出,然后缩小font-sizepaddingmargin值。 它看起来与抹布并不相同,但是在没有静态heightmax-height情况下仍可以工作。

工作示例:

 /* final display */ #menu #list { margin: .5em 1em; padding: 1em; } /* hide */ #menu:not(:hover) #list { font-size: 0; margin: 0; opacity: 0; padding: 0; /* fade out, then shrink */ transition: opacity .25s, font-size .5s .25s, margin .5s .25s, padding .5s .25s; } /* reveal */ #menu:hover #list { /* unshrink, then fade in */ transition: font-size .25s, margin .25s, padding .25s, opacity .5s .25s; } 
 <div id="menu"> <b>hover me</b> <ul id="list"> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul> </div> <p>Another paragraph...</p> 

#14楼

您可以用一点点非语义的拼图扑克。 我通常的方法是为外部DIV的高度设置动画,该外部DIV具有单个子对象,这是一种无样式的DIV,仅用于测量内容高度。

 function growDiv() { var growDiv = document.getElementById('grow'); if (growDiv.clientHeight) { growDiv.style.height = 0; } else { var wrapper = document.querySelector('.measuringWrapper'); growDiv.style.height = wrapper.clientHeight + "px"; } } 
 #grow { -moz-transition: height .5s; -ms-transition: height .5s; -o-transition: height .5s; -webkit-transition: height .5s; transition: height .5s; height: 0; overflow: hidden; outline: 1px solid red; } 
 <input type="button" onclick="growDiv()" value="grow"> <div id='grow'> <div class='measuringWrapper'> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> </div> </div> 

一个人只希望可以省去.measuringWrapper而只是将DIV的高度设置为auto并设置动画,但这似乎不起作用(设置了高度,但没有动画发生)。

 function growDiv() { var growDiv = document.getElementById('grow'); if (growDiv.clientHeight) { growDiv.style.height = 0; } else { growDiv.style.height = 'auto'; } } 
 #grow { -moz-transition: height .5s; -ms-transition: height .5s; -o-transition: height .5s; -webkit-transition: height .5s; transition: height .5s; height: 0; overflow: hidden; outline: 1px solid red; } 
 <input type="button" onclick="growDiv()" value="grow"> <div id='grow'> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> <div> The contents of my div. </div> </div> 

我的解释是动画运行需要明确的高度。 当height(开始高度或结束高度)均为auto时,无法在height上获得动画。


#15楼

好的,所以我想我想出了一个非常简单的答案... no max-height ,使用relative定位,在li元素上工作,并且是纯CSS。 除了Firefox之外,我没有在其他任何软件上进行过测试,尽管从CSS来看,它应该可以在所有浏览器上使用。

内容: http : //jsfiddle.net/n5XfG/2596/

的CSS

.wrap { overflow:hidden; }.inner {margin-top:-100%;-webkit-transition:margin-top 500ms;transition:margin-top 500ms;
}.inner.open { margin-top:0px; }

的HTML

<div class="wrap"><div class="inner">Some Cool Content</div>
</div>

#16楼

当前涉及的高度之一是auto ,您当前无法在高度上设置动画,您必须设置两个显式的高度。


#17楼

没有硬编码的值。

没有JavaScript。

没有近似值。

诀窍是使用隐藏和重复的div来使浏览器了解100%的含义。

只要您能够复制要设置动画的元素的DOM,此方法就适用。

 .outer { border: dashed red 1px; position: relative; } .dummy { visibility: hidden; } .real { position: absolute; background: yellow; height: 0; transition: height 0.5s; overflow: hidden; } .outer:hover>.real { height: 100%; } 
 Hover over the box below: <div class="outer"> <!-- The actual element that you'd like to animate --> <div class="real"> unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content </div> <!-- An exact copy of the element you'd like to animate. --> <div class="dummy" aria-hidden="true"> unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content unpredictable content </div> </div> 

#18楼

几乎没有提到Element.scrollHeight属性,该属性在这里可能有用,但仍然可以与纯CSS过渡一起使用。 该属性始终包含元素的“完整”高度,无论其内容是否由于高度折叠(例如, height: 0 )而溢出以及如何溢出。

这样,对于height: 0height: 0 (有效完全折叠)的元素,仍可以通过其scrollHeight值(始终为像素长度)轻松获得其“正常”或“完整”高度。

对于这样的元素,假设它已经设置了转换,例如(根据原始问题使用ul ):

ul {height: 0;transition: height 1s; /* An example transition. */
}

我们可以仅使用CSS触发所需的动画高度“扩展”,如下所示(此处假设ul变量引用列表):

ul.style.height = ul.scrollHeight + "px";

而已。 如果您需要折叠列表,则可以使用以下两个语句之一:

ul.style.height = "0";
ul.style.removeProperty("height");

我的特定用例围绕动画列表,这些动画列表的长度通常未知且通常相当长,因此我不愿意选择任意“足够大”的heightmax-height规格,并且冒着切入内容或突然需要滚动的内容的风险(如果overflow: auto例如, overflow: auto )。 此外,由于基于max-height的解决方案破坏了缓动和计时,因为所使用的高度可能比max-height达到9999px时间要早得多。 随着屏幕分辨率的提高,像9999px这样的像素长度在我的口中留下了不良的味道。 我认为,这种特殊的解决方案以一种优雅的方式解决了这个问题。

最后,这里希望CSS的未来修订版能够满足作者的要求,使他们更加优雅地做这些事情-重新审视“ compute”与“ used”和“ resolved”值的概念,并考虑是否应将过渡应用于计算值,包括具有widthheight过渡(目前有一些特殊处理)。


#19楼

这并不是解决问题的“解决方案”,而是更多的解决方法。 我确定它只能与文本一起使用,但是可以根据需要更改为与其他元素一起使用。

.originalContent {font-size:0px;transition:font-size .2s ease-in-out;
}
.show { /* class to add to content */font-size:14px;
}

这是一个示例: http : //codepen.io/overthemike/pen/wzjRKa

本质上,您将font-size设置为0并以足够快的速度转换它而不是高度,max-height或scaleY()等,以使高度转换为所需的高度。 当前无法使用CSS将实际高度转换为自动,但是可以转换其中的内容,因此可以转换字体大小。

  • 注意-Codepen中有JavaScript,但唯一的目的是单击时添加/删除CSS类的手风琴。 这可以通过隐藏的单选按钮完成,但我并没有专注于此,只是高度转换。

#20楼

发布此帖子时,已经有30多个答案,但是我觉得我的答案比jake早已接受的答案有所改善。

我不满足于仅使用max-height和CSS3过渡所引起的问题,因为正如许多评论者所指出的那样,您必须将max-height值设置为非常接近实际高度,否则会出现延迟。 有关该问题的示例,请参见此JSFiddle 。

为了解决这个问题(尽管仍然不使用JavaScript),我添加了另一个HTML元素来转换transform: translateY CSS值。

这意味着同时使用了max-heighttranslateYmax-height允许元素将其下的元素下推,而translateY提供我们想要的“即时”效果。 max-height仍然存在,但其影响有所减轻。 这意味着您可以为max-height值设置更大的高度,而不必担心。

总体好处是,在返回(折叠)的过渡过程中,用户可以立即看到translateY动画,因此, max-height花多长时间并不重要。

解决作为小提琴

 body { font-family: sans-serif; } .toggle { position: relative; border: 2px solid #333; border-radius: 3px; margin: 5px; width: 200px; } .toggle-header { margin: 0; padding: 10px; background-color: #333; color: white; text-align: center; cursor: pointer; } .toggle-height { background-color: tomato; overflow: hidden; transition: max-height .6s ease; max-height: 0; } .toggle:hover .toggle-height { max-height: 1000px; } .toggle-transform { padding: 5px; color: white; transition: transform .4s ease; transform: translateY(-100%); } .toggle:hover .toggle-transform { transform: translateY(0); } 
 <div class="toggle"> <div class="toggle-header"> Toggle! </div> <div class="toggle-height"> <div class="toggle-transform"> <p>Content!</p> <p>Content!</p> <p>Content!</p> <p>Content!</p> </div> </div> </div> <div class="toggle"> <div class="toggle-header"> Toggle! </div> <div class="toggle-height"> <div class="toggle-transform"> <p>Content!</p> <p>Content!</p> <p>Content!</p> <p>Content!</p> </div> </div> </div> 

#21楼

似乎没有适当的解决方案。 max-height方法非常好,但在隐藏阶段效果不佳-除非您知道内容的高度,否则会有明显的延迟。

我认为最好的方法是使用max-height仅用于show阶段 。 并且不要在隐藏时使用任何动画。 在大多数情况下,它不是至关重要的。

应该将max-height设置为一个很大的值,以确保任何内容都适合。 动画速度可以使用transition持续时间来控制( speed = max-height / duration )。 速度不取决于内容的大小。 显示整个内容所需的时间取决于其大小。

 document.querySelector("button").addEventListener( "click", function(){ document.querySelector("div").classList.toggle("hide"); } ) 
 div { max-height: 20000px; transition: max-height 3000ms; overflow-y: hidden; } .hide { max-height: 0; transition: none; } 
 <button>Toggle</button> <div class="hide">Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. Lorem ipsum dolor sit amet, ius solet dignissim honestatis ad. Mea quem tibique intellegat te. Insolens deterruisset cum ea. Te omnes percipit consulatu eos. Vix novum primis salutatus no, eam denique sensibus et, his ipsum senserit ne. </div> 

#22楼

我知道这是这个问题的答案,但我认为这是值得的,所以就去吧。 这是具有以下属性的纯CSS解决方案:

  • 一开始没有延迟,过渡也不会尽早停止。 在两个方向(展开和折叠)中,如果您在CSS中指定了300ms的过渡持续时间,则过渡需要300ms的时间。
  • 它正在转换实际高度(与transform: scaleY(0) ),因此,如果可折叠元素后面有内容,它会做正确的事情。
  • 虽然(像其他的解决方案), 神奇的号码(如“选择一个长度比你的框永远将会是更高的”),这不是致命的,如果你的假设最终被错。 在这种情况下,过渡效果可能看起来并不惊人,但是在过渡之前和之后,这都不成问题:在扩展状态( height: auto )下,整个内容始终具有正确的高度(例如,如果您选择max-height过低)。 在折叠状态下,高度应为零。

演示版

这是一个具有三个可折叠元素的演示,这些元素都具有不同的高度,并且都使用相同的CSS。 您可能要在单击“运行摘要”后单击“整页”。 请注意,JavaScript仅切换collapsed CSS类,不涉及任何度量。 (您可以使用复选框或:target来进行完全没有任何JavaScript的演示,)。 还要注意,CSS中负责过渡的部分非常短,而HTML仅需要一个附加的wrapper元素。

 $(function () { $(".toggler").click(function () { $(this).next().toggleClass("collapsed"); $(this).toggleClass("toggled"); // this just rotates the expander arrow }); }); 
 .collapsible-wrapper { display: flex; overflow: hidden; } .collapsible-wrapper:after { content: ''; height: 50px; transition: height 0.3s linear, max-height 0s 0.3s linear; max-height: 0px; } .collapsible { transition: margin-bottom 0.3s cubic-bezier(0, 0, 0, 1); margin-bottom: 0; max-height: 1000000px; } .collapsible-wrapper.collapsed > .collapsible { margin-bottom: -2000px; transition: margin-bottom 0.3s cubic-bezier(1, 0, 1, 1), visibility 0s 0.3s, max-height 0s 0.3s; visibility: hidden; max-height: 0; } .collapsible-wrapper.collapsed:after { height: 0; transition: height 0.3s linear; max-height: 50px; } /* END of the collapsible implementation; the stuff below is just styling for this demo */ #container { display: flex; align-items: flex-start; max-width: 1000px; margin: 0 auto; } .menu { border: 1px solid #ccc; box-shadow: 0 1px 3px rgba(0,0,0,0.5); margin: 20px; } .menu-item { display: block; background: linear-gradient(to bottom, #fff 0%,#eee 100%); margin: 0; padding: 1em; line-height: 1.3; } .collapsible .menu-item { border-left: 2px solid #888; border-right: 2px solid #888; background: linear-gradient(to bottom, #eee 0%,#ddd 100%); } .menu-item.toggler { background: linear-gradient(to bottom, #aaa 0%,#888 100%); color: white; cursor: pointer; } .menu-item.toggler:before { content: ''; display: block; border-left: 8px solid white; border-top: 8px solid transparent; border-bottom: 8px solid transparent; width: 0; height: 0; float: right; transition: transform 0.3s ease-out; } .menu-item.toggler.toggled:before { transform: rotate(90deg); } body { font-family: sans-serif; font-size: 14px; } *, *:after { box-sizing: border-box; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div class="menu"> <div class="menu-item">Something involving a holodeck</div> <div class="menu-item">Send an away team</div> <div class="menu-item toggler">Advanced solutions</div> <div class="collapsible-wrapper collapsed"> <div class="collapsible"> <div class="menu-item">Separate saucer</div> <div class="menu-item">Send an away team that includes the captain (despite Riker's protest)</div> <div class="menu-item">Ask Worf</div> <div class="menu-item">Something involving Wesley, the 19th century, and a holodeck</div> <div class="menu-item">Ask Q for help</div> </div> </div> <div class="menu-item">Sweet-talk the alien aggressor</div> <div class="menu-item">Re-route power from auxiliary systems</div> </div> <div class="menu"> <div class="menu-item">Something involving a holodeck</div> <div class="menu-item">Send an away team</div> <div class="menu-item toggler">Advanced solutions</div> <div class="collapsible-wrapper collapsed"> <div class="collapsible"> <div class="menu-item">Separate saucer</div> <div class="menu-item">Send an away team that includes the captain (despite Riker's protest)</div> </div> </div> <div class="menu-item">Sweet-talk the alien aggressor</div> <div class="menu-item">Re-route power from auxiliary systems</div> </div> <div class="menu"> <div class="menu-item">Something involving a holodeck</div> <div class="menu-item">Send an away team</div> <div class="menu-item toggler">Advanced solutions</div> <div class="collapsible-wrapper collapsed"> <div class="collapsible"> <div class="menu-item">Separate saucer</div> <div class="menu-item">Send an away team that includes the captain (despite Riker's protest)</div> <div class="menu-item">Ask Worf</div> <div class="menu-item">Something involving Wesley, the 19th century, and a holodeck</div> <div class="menu-item">Ask Q for help</div> <div class="menu-item">Separate saucer</div> <div class="menu-item">Send an away team that includes the captain (despite Riker's protest)</div> <div class="menu-item">Ask Worf</div> <div class="menu-item">Something involving Wesley, the 19th century, and a holodeck</div> <div class="menu-item">Ask Q for help</div> </div> </div> <div class="menu-item">Sweet-talk the alien aggressor</div> <div class="menu-item">Re-route power from auxiliary systems</div> </div> </div> 

它是如何工作的?

实际上,要实现这一目标涉及两个过渡。 其中之一将margin-bottom从0px(处于展开状态)转换为-2000px (处于折叠状态)(类似于此答案 )。 这里的2000是第一个魔幻数字,它基于您的框不会高于此数字的假设(2000像素似乎是一个合理的选择)。

仅单独使用margin-bottom过渡有两个问题:

  • 如果您实际上有一个高于2000像素的框,则margin-bottom: -2000px不会隐藏所有内容-即使在折叠的情况下也会有可见的东西。 这是一个较小的修复程序,我们将在以后进行。
  • 例如,如果实际的框高为1000像素,并且过渡的长度为300ms,则可见过渡在大约150ms后已经结束(或者,在相反的方向,延迟时间为150ms)。

解决第二个问题是第二个转换的到来,这个转换从概念上讲针对包装器的最小高度(“概念上”,因为我们实际上并未为此使用min-height属性;稍后将进行介绍)。

这是一个动画,展示了如何将底部边距过渡与持续时间相等的最小高度过渡组合在一起,从而为我们提供了从全高到持续时间相同的零高度的组合过渡。

左栏显示负底边距如何向上推动底部,从而减小可见高度。 中间的条显示了最小高度如何确保在折叠情况下过渡不会提前结束,而在扩展情况下过渡不会迟到。 右栏显示了两者的组合如何导致框在正确的时间内从全高过渡到零高。

对于我的演示,我将50px设置为最小最小高度的上限。 这是第二个魔术数字,它应该低于盒子的高度。 50px也很合理; 您似乎不太可能经常想使一个可折叠的元素起初甚至不超过50像素。

正如您在动画中所看到的那样,最终的过渡是连续的,但不可区分-在最小高度等于由底部边距调整的全高度的那一刻,速度会突然变化。 这在动画中非常明显,因为它对两个过渡都使用线性计时功能,并且因为整个过渡非常慢。 在实际情况下(我的演示在顶部),过渡仅花费300ms,底部边距过渡不是线性的。 对于这两种转换,我都使用了许多不同的计时功能,而最终我觉得它们在最广泛的情况下效果最好。

还有两个问题需要解决:

  1. 从上方的角度来看,高度超过2000像素的框在折叠状态下并未完全隐藏,
  2. 相反的问题是,在非隐藏情况下,即使不运行过渡,小于50像素高度的框也会过高,因为最小高度将其保持在50像素。

我们通过为容器元素赋予max-height: 0在折叠的情况下为0s 0.3s过渡为0s 0.3s解决第一个问题。 这意味着它并不是真正的过渡,但是max-height会延迟应用; 它仅在过渡结束后才适用。 为了使它正常工作,我们还需要为相反的,未折叠的状态选择一个数值max-height 。 但是与2000px情况不同的是,选择太大的数字会影响过渡的质量,在这种情况下,这实际上并不重要。 因此,我们只能选择一个很高的数字,以至于我们知道没有一个高度会接近这个数字。 我选了一百万像素。 如果您觉得可能需要支持超过一百万像素的高度的内容,则1)对不起,2)仅添加几个零。

第二个问题是为什么我们实际上没有使用min-height来进行最小高度转换。 相反,容器中有一个::after伪元素,其height从50px过渡到零。 这与min-height具有相同的效果:不会让容器缩小到伪元素当前具有的任何高度以下。 但是因为我们使用的是height而不是min-height ,所以我们现在可以使用max-height (再次应用延迟)将过渡结束后将伪元素的实际高度设置为零,从而确保至少在过渡,即使很小的元素也具有正确的高度。 由于min-height为强比max-height ,如果我们使用容器的这是行不通的min-height ,而不是伪元素的height 。 就像上一段中的max-height一样,此max-height在过渡的另一端也需要一个值。 但是在这种情况下,我们可以选择50px。

已在Chrome(Win,Mac,Android,iOS),Firefox(Win,Mac,Android),Edge,IE11(我的演示中没有麻烦调试的Flexbox布局问题除外)和Safari(Mac,iOS)中经过测试)。 说到flexbox,应该有可能不使用任何flexbox就可以完成这项工作; 实际上,我认为您几乎可以使IE7中的所有功能正常运行-除了您没有CSS转换这一事实之外,这是相当没有意义的练习。


#23楼

Flexbox解决方案

优点:

  • 简单
  • 没有JS
  • 平稳过渡

缺点:

  • 元素需要放在固定高度的伸缩容器中

它的工作方式是始终使用flex-basis:在具有内容的元素上自动放置,然后过渡flex-grow和flex-shrink。

编辑:受Xbox One界面启发的改进的JS Fiddle。

 * { margin: 0; padding: 0; box-sizing: border-box; transition: 0.25s; font-family: monospace; } body { margin: 10px 0 0 10px; } .box { width: 150px; height: 150px; margin: 0 2px 10px 0; background: #2d333b; border: solid 10px #20262e; overflow: hidden; display: inline-flex; flex-direction: column; } .space { flex-basis: 100%; flex-grow: 1; flex-shrink: 0; } p { flex-basis: auto; flex-grow: 0; flex-shrink: 1; background: #20262e; padding: 10px; width: 100%; text-align: left; color: white; } .box:hover .space { flex-grow: 0; flex-shrink: 1; } .box:hover p { flex-grow: 1; flex-shrink: 0; } 
 <div class="box"> <div class="space"></div> <p> Super Metroid Prime Fusion </p> </div> <div class="box"> <div class="space"></div> <p> Resident Evil 2 Remake </p> </div> <div class="box"> <div class="space"></div> <p> Yolo The Game </p> </div> <div class="box"> <div class="space"></div> <p> Final Fantasy 7 Remake + All Additional DLC + Golden Tophat </p> </div> <div class="box"> <div class="space"></div> <p> DerpVille </p> </div> 

JS小提琴


#24楼

我了解这个问题要求没有JavaScript的解决方案。 但是对于那些感兴趣的人,这里是我的解决方案,仅使用少量JS。

好的,因此默认情况下高度将更改的元素的css设置为height: 0; 当打开height: auto; 。 它还具有transition: height .25s ease-out; 。 但是,当然,问题在于它不会向高处过渡height: auto;

所以我要做的是在打开或关闭时将高度设置为元素的scrollHeight属性。 这种新的内联样式将具有更高的特异性并覆盖两个height: auto;height: 0; 然后过渡运行。

打开时,我添加了一个transitionend事件侦听器,该侦听器将只运行一次,然后删除将其内联样式设置为height: auto;的内联样式height: auto; 这将允许元素在必要时调整大小,例如在这个带有子菜单https://codepen.io/ninjabonsai/pen/GzYyVe的更复杂的示例中

关闭时,我将在下一个事件循环周期之后立即使用setTimeout毫不延迟地删除内联样式。 这意味着height: auto; 暂时被覆盖,允许转换回height 0;

 const showHideElement = (element, open) => { element.style.height = element.scrollHeight + 'px'; element.classList.toggle('open', open); if (open) { element.addEventListener('transitionend', () => { element.style.removeProperty('height'); }, { once: true }); } else { window.setTimeout(() => { element.style.removeProperty('height'); }); } } const menu = document.body.querySelector('#menu'); const list = document.body.querySelector('#menu > ul') menu.addEventListener('mouseenter', () => showHideElement(list, true)); menu.addEventListener('mouseleave', () => showHideElement(list, false)); 
 #menu>ul { height: 0; overflow: hidden; background-color: #999; transition: height .25s ease-out; } #menu>ul.open { height: auto; } 
 <div id="menu"> <a>hover me</a> <ul> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul> </div> 

#25楼

您可以从height:0转换为height:auto,前提是您还要提供min-height和max-height。

div.stretchy{transition: 1s linear;
}div.stretchy.hidden{height: 0;
}div.stretchy.visible{height: auto;min-height:40px;max-height:400px;
}

#26楼

我发布了一些JavaScript的答案,并被否决了,因此感到恼火并再次尝试,仅使用CSS破解了它!

该解决方案使用了一些“技术”:

  • padding-bottom:100% 'hack',其中百分比是根据元素的当前宽度定义的。 有关此技术的更多信息。
  • 浮动收缩包装,(需要额外的div来应用浮动清除技巧)
  • 对https://caniuse.com/#feat=css-writing-mode的非语义化使用和一些转换来撤消它(这允许在垂直上下文中使用上面的padding hack)

结果是,我们仅使用CSS即可进行高效的转换,并且可以使用一个转换功能来平稳地实现转换。 圣杯!

当然,还有一个缺点! 我不知道如何控制内容被截断的宽度( overflow:hidden ); 由于存在底部填充漏洞,因此宽度和高度密切相关。 也许有办法,所以会回到正题。

https://jsfiddle.net/EoghanM/n1rp3zb4/28/

 body { padding: 1em; } .trigger { font-weight: bold; } /* .expander is there for float clearing purposes only */ .expander::after { content: ''; display: table; clear: both; } .outer { float: left; /* purpose: shrink to fit content */ border: 1px solid green; overflow: hidden; } .inner { transition: padding-bottom 0.3s ease-in-out; /* or whatever crazy transition function you can come up with! */ padding-bottom: 0%; /* percentage padding is defined in terms of width. The width at this level is equal to the height of the content */ height: 0; /* unfortunately, change of writing mode has other bad effects like orientation of cursor */ writing-mode: vertical-rl; cursor: default; /* don't want the vertical-text (sideways I-beam) */ transform: rotate(-90deg) translateX(-100%); /* undo writing mode */ transform-origin: 0 0; margin: 0; /* left/right margins here will add to height */ } .inner > div { white-space: nowrap; } .expander:hover .inner, /* to keep open when expanded */ .trigger:hover+.expander .inner { padding-bottom: 100%; } 
 <div class="trigger">HoverMe</div> <div class="expander"> <div class="outer"> <div class="inner"> <div>First Item</div> <div>Content</div> <div>Content</div> <div>Content</div> <div>Long Content can't be wider than outer height unfortunately</div> <div>Last Item</div> </div> </div> </div> <div> after content</div> </div> 

#27楼

我知道该线程已经过时,但是在某些Google搜索中它的排名很高,因此我认为值得进行更新。

您还可以获取/设置元素自己的高度:

var load_height = document.getElementById('target_box').clientHeight;
document.getElementById('target_box').style.height = load_height + 'px';

您应该在内联脚本标记中的target_box的结束标记之后立即转储此Javascript。


#28楼

使用CSS3过渡为高度设置动画的一种可视方法是改为对填充进行动画处理。

您还不能完全获得擦除效果,但是使用过渡时间和填充值应该可以使您足够接近。 如果您不想显式设置height / max-height,这应该是您想要的。

div {height: 0;overflow: hidden;padding: 0 18px;-webkit-transition: all .5s ease;-moz-transition: all .5s ease;transition: all .5s ease;
}
div.animated {height: auto;padding: 24px 18px;
}

http://jsfiddle.net/catharsis/n5XfG/17/ (从stephband的jsFiddle上方摘下来)


#29楼

这是我刚与jQuery结合使用的解决方案。 这适用于以下HTML结构:

<nav id="main-nav"><ul><li><a class="main-link" href="yourlink.html">Link</a><ul><li><a href="yourlink.html">Sub Link</a></li></ul></li></ul>
</nav>

和功能:

    $('#main-nav li ul').each(function(){$me = $(this);//Count the number of li elements in this ULvar liCount = $me.find('li').size(),//Multiply the liCount by the height + the margin on each liulHeight = liCount * 28;//Store height in the data-height attribute in the UL$me.attr("data-height", ulHeight);});

然后,您可以使用click函数通过css()设置和删除高度

$('#main-nav li a.main-link').click(function(){//Collapse all submenus back to 0$('#main-nav li ul').removeAttr('style');$(this).parent().addClass('current');//Set height on current submenu to it's heightvar $currentUl = $('li.current ul'),currentUlHeight = $currentUl.attr('data-height');
})

CSS:

#main-nav li ul { height: 0;position: relative;overflow: hidden;opacity: 0; filter: alpha(opacity=0); -ms-filter: "alpha(opacity=0)";-khtml-opacity: 0; -moz-opacity: 0;-webkit-transition: all .6s ease-in-out;-moz-transition: all .6s ease-in-out;-o-transition: all .6s ease-in-out;-ms-transition: all .6s ease-in-out;transition: all .6s ease-in-out;
}#main-nav li.current ul {opacity: 1.0; filter: alpha(opacity=100); -ms-filter: "alpha(opacity=100)";-khtml-opacity: 1.0; -moz-opacity: 1.0;
}.ie #main-nav li.current ul { height: auto !important }#main-nav li { height: 25px; display: block; margin-bottom: 3px }

#30楼

在过渡中使用max-height ,而不要使用height 。 并将max-height的值设置为比您的盒子所能达到的更大的值。

请参阅Chris Jordan提供的JSFiddle演示 ,在此处还有另一个答案 。

 #menu #list { max-height: 0; transition: max-height 0.15s ease-out; overflow: hidden; background: #d5d5d5; } #menu:hover #list { max-height: 500px; transition: max-height 0.25s ease-in; } 
 <div id="menu"> <a>hover me</a> <ul id="list"> <!-- Create a bunch, or not a bunch, of li's to see the timing. --> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul> </div> 

#31楼

我的解决方法是将max-height转换为精确平滑的内容高度,然后使用transitionEnd回调将max-height设置为9999px,以便内容可以自由调整大小。

 var content = $('#content'); content.inner = $('#content .inner'); // inner div needed to get size of content when closed // css transition callback content.on('transitionEnd webkitTransitionEnd transitionend oTransitionEnd msTransitionEnd', function(e){ if(content.hasClass('open')){ content.css('max-height', 9999); // try setting this to 'none'... I dare you! } }); $('#toggle').on('click', function(e){ content.toggleClass('open closed'); content.contentHeight = content.outerHeight(); if(content.hasClass('closed')){ // disable transitions & set max-height to content height content.removeClass('transitions').css('max-height', content.contentHeight); setTimeout(function(){ // enable & start transition content.addClass('transitions').css({ 'max-height': 0, 'opacity': 0 }); }, 10); // 10ms timeout is the secret ingredient for disabling/enabling transitions // chrome only needs 1ms but FF needs ~10ms or it chokes on the first animation for some reason }else if(content.hasClass('open')){ content.contentHeight += content.inner.outerHeight(); // if closed, add inner height to content height content.css({ 'max-height': content.contentHeight, 'opacity': 1 }); } }); 
 .transitions { transition: all 0.5s ease-in-out; -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; } body { font-family:Arial; line-height: 3ex; } code { display: inline-block; background: #fafafa; padding: 0 1ex; } #toggle { display:block; padding:10px; margin:10px auto; text-align:center; width:30ex; } #content { overflow:hidden; margin:10px; border:1px solid #666; background:#efefef; opacity:1; } #content .inner { padding:10px; overflow:auto; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <div id="content" class="open"> <div class="inner"> <h3>Smooth CSS Transitions Between <code>height: 0</code> and <code>height: auto</code></h3> <p>A clever workaround is to use <code>max-height</code> instead of <code>height</code>, and set it to something bigger than your content. Problem is the browser uses this value to calculate transition duration. So if you set it to <code>max-height: 1000px</code> but the content is only 100px high, the animation will be 10x too fast.</p> <p>Another option is to measure the content height with JS and transition to that fixed value, but then you have to keep track of the content and manually resize it if it changes.</p> <p>This solution is a hybrid of the two - transition to the measured content height, then set it to <code>max-height: 9999px</code> after the transition for fluid content sizing.</p> </div> </div> <br /> <button id="toggle">Challenge Accepted!</button> 

如何转换高度:0; 达到高度:自动; 使用CSS?相关推荐

  1. css纵向滚动条自适应,纯css实现完美决对居中 自动滚动条 根据内容宽度、高度自适应,垂直水平居中,自动出现滚动条。.doc...

    纯css实现完美决对居中 自动滚动条 根据内容宽度.高度自适应,垂直水平居中,自动出现滚动条..doc 还剩 3页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,喜欢就下载吧,价低环保! 内 ...

  2. php设置文本框高度,怎样让textarea文本域宽度和高度width及height自动适应

    这次给大家带来怎样让textarea文本域宽度和高度width及height自动适应,让textarea文本域宽度和高度width及height自动适应的注意事项有哪些,下面就是实战案例,一起来看一下 ...

  3. html div 自动适应屏幕,css怎么让高度自适应屏幕高度?

    在一个DIV中有多个未知高度的子元素DIV,怎样通过CSS实现子元素div同高或怎样实现同行div按照内容最多的那个设置高度.下面我们来看一下css设置高度自适应屏幕高度的方法. 首先,我们刚开始设计 ...

  4. Unity Shader PostProcessing - 11 - Depth Fog/Height Fog - 雾效/深度雾效/高度雾/深度+高度雾

    文章目录 观察生活中的雾 Scenario 1 - Pure Depth Fog - 纯深度雾 提取有用信息 Scenario 2 - Height Fog - 带高度的雾 提取有用信息 Scenar ...

  5. html 软键盘 页面高度,深入理解高度,获取屏幕、webview、软键盘高度

    如果没有手动调整过webview的高度的话,默认情况下,屏幕的高度=顶部状态栏的高度+webview的高度. 如果软键盘打开,则屏幕的高度=顶部状态栏的高度+webview的高度+软键盘的高度. HT ...

  6. css实现容器高度 适应 屏幕高度

    <html>元素的高度默认是auto,被内容自动撑开: 100%:使得html的height与屏幕的高度相等: 50%:使得html的height等于屏幕的一半: 若想让一个<div ...

  7. Php的if自动转换类型,php之数据类型自动转换,php之数据类型转换_PHP教程

    php之数据类型自动转换,php之数据类型转换 1:概述 ---php是一种弱类型的语言,它可以根据运行环境的变化而自动进行数据类型的转换 1.1转换成布尔类型的原则 以下值都将转换成布尔类型中的fa ...

  8. js设置div高度低于滚动高度时固定

    今天在电脑端看csdn时,发现文章界面一般分为三大块:左边侧边栏显示用户基本信息和广告.中间文章部分.右边用户的文章专栏.整体用的是float.右边的文章position设置为fixed.左边侧边栏是 ...

  9. php自动转换和强制转换,Java数据类型转换(自动转换和强制转换)

    数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中"悄然"进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换;强制类型转换则必须在代码中声明,转 ...

最新文章

  1. 仿余额宝数字跳动效果 TextCounter
  2. 【PAT乙级】1065 单身狗 (25 分)
  3. html图片旋转代码_HTML设计一个小程序
  4. BZOJ 1070 修车
  5. C++ 11 新特性(十二)函数新特性、内联函数、const详解
  6. Android逆向笔记-IDA Pro动态调试Android程序(真机)
  7. ZOJ 3490 String Successor
  8. python actor_Python定义一个Actor任务
  9. 小技巧 ----- 关于Java中的System.arraycopy()
  10. slice python_Python slice()
  11. 转帖一则——从医学角度论成熟
  12. Selenium Grid
  13. nodejs返回带图片的HTML页面,Nodejs实现简单的网页图片获取
  14. 数据结构之队列(链式队列)的基本操作与实现
  15. [附源码]Java计算机毕业设计SSM电商直播订单管理系统
  16. linux下drcom无法上网,安装drcom出现问题
  17. Python图书管理系统(终章)
  18. 龙芯3a5000下编译redis 7.0源码
  19. AlertPay网银
  20. EPICS工具CSS学习笔记

热门文章

  1. android 上传到了maven,但是报错找不到jar
  2. Android toolbar menu 字体点击样式
  3. android studio第一次配置flutter(2019年12月)
  4. java同步机制:synchronized
  5. EventBus3.0源码解析
  6. C语言函数集(十三)
  7. Kafka Sender线程如何发送数据
  8. 线程池之CachedThreadPool学习
  9. 【java】用户动态代理
  10. HEOI2016/TJOI2016 字符串问题