简体   繁体   English

最大高度过渡问题

[英]Max-height transition problems

I make autocomplete Max-height set from JavaScript: 我通过JavaScript设置自动完成的最大高度:

if (data.length < 10) 
    element.css({'max-height': (30 * newVal.length) + 'px'})

If max-height decreases(eg 300px to 150px), transition does not work. 如果最大高度减小(例如,从300px变为150px),则过渡无效。

If max-height increases(eg 150px to 300px), transition works. 如果最大高度增加(例如150px至300px),则过渡有效。

.autocomplete-ion {
 background-color: gray;
 position: absolute;
 width: 90%;
 left: 5%;
 top:45px;
 overflow-y: auto;
 z-index: 10000000;
 background-color: #FAFAFA;
 transition: 0.8s;
 max-height: 300px;
 box-shadow: 0 3px 1px -2px rgba(0,0,0,.14),0 2px 2px 0 rgba(0,0,0,.098),0 1px 5px 0 rgba(0,0,0,.084);
 ul li {
   padding:5px;
 }
}

It is because you have max-height value as 300px in your css. 这是因为CSS中的max-height值为300px。 So you should remove that to work properly 所以您应该删除它才能正常工作

.autocomplete-ion {
 background-color: gray;
 position: absolute;
 width: 90%;
 left: 5%;
 top:45px;
 overflow-y: auto;
 z-index: 10000000;
 background-color: #FAFAFA;
 transition: 0.8s;
 box-shadow: 0 3px 1px -2px rgba(0,0,0,.14),0 2px 2px 0 rgba(0,0,0,.098),0 1px 5px 0 rgba(0,0,0,.084);
 ul li {
   padding:5px;
 }

Because the height change from element add/remove will not cause a animation. 因为从element添加/删除中更改height不会导致动画。

When elements increasing, the new height is likely to always larger than previous max-height , so when you set a higher max-height , the animation appears from old max-height to new one. 当元素增加时,新的height可能总是大于以前的max-height ,因此,当您设置更高的max-height ,动画会从旧的max-height到新的max-height

When elements decreasing, if you remove the elements first, then the height will decrease first, without animation, then, when you set the new max-height , it'll only animate part only if new max-height is smaller than decreased height. 当元素减少时,如果先删除元素,则高度将先减少,而没有动画,然后,当您设置新的max-height ,仅当新的max-height小于减小max-height ,它才会使零件动画。 And if the new max-height is still larger than decreased height, the animation not appears at all. 并且如果新的max-height仍大于减小的高度,则动画根本不会出现。

So you have to first set to the new max-height when new elements is less then old ones, to trigger animation, and set the list to new one(either by removing or create a new list) when animation ends. 因此,必须在新元素小于旧元素时首先将其设置为新的max-height ,以触发动画,并在动画结束时将列表设置为新元素(通过删除或创建新列表)。

 var list = $(".autocomplete-ion ul"); var tryAppend = function(newList) { var curList = $(".autocomplete-ion ul li"); var curLength = curList.length; var newLength = newList.length; if (newLength <= 10) { // If its adding, no need to listen to animation, as the new height will be definetly larger. // Otherwise, if (newLength < curLength) { $(".autocomplete-ion").on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function() { list.empty().append(newList); $(this).off(); }); $(".autocomplete-ion").css({'max-height': (30 * newLength) + 'px'}); } else { $(".autocomplete-ion").css({'max-height': (30 * newLength) + 'px'}); list.empty().append(newList); } } }; var create = function(num) { var list =[]; var i, li; for (i = 0; i < num; ++i ) { li = $("<li>").text("Test li " + (i + 1)); list.push(li); } tryAppend(list); }; $(".cl").click(function() { var counts = parseInt($(this).data("len"), 10); create(counts); }); $(".clear").click(function() { list.empty(); }); 
 .autocomplete-ion { background-color: gray; position: absolute; width: 90%; left: 5%; top:45px; overflow-y: auto; z-index: 10000000; background-color: #FAFAFA; transition: 0.8s; max-height: 0px; box-shadow: 0 3px 1px -2px rgba(0,0,0,.14),0 2px 2px 0 rgba(0,0,0,.098),0 1px 5px 0 rgba(0,0,0,.084); ul li { height: 40px; padding:5px; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="autocomplete-ion"> <ul> </ul> </div> <button class="cl" data-len="1">1</button> <button class="cl" data-len="5">5</button> <button class="cl" data-len="10">10</button> 

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

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