找回密码
 立即注册
首页 业界区 业界 不定高元素动画实现方案(上)

不定高元素动画实现方案(上)

溥价 2025-9-20 16:35:09
前情

最近接了一个需求,需要实现一个列表,列表可展开收起,展开收起需要有一个动画效果,而列表个数不定且每项内容高度也不固定,所以是一个不定高的收起展开效果,我尝试了如下几中方式,最后选择了我觉得最佳的
通过max-height来实现

其实把高度设为auto,可以让元素高度撑开,但是auto是不会有动画效果的,此时我们可以把max-height设为一个较大的值,这样就实现动画效果了
代码如下:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.   
  10.    
  11.       header
  12.       <ul >
  13.         <li>111111111</li>
  14.         <li>2222222222</li>
  15.         <li>333333333</li>
  16.       </ul>
  17.    
  18.   
  19. </body>
  20. </html>
复制代码
  1. *{
  2.   margin: 0;
  3.   padding: 0;
  4. }
  5. .container{
  6.   width: 100%;
  7.   max-height: 48px;
  8.   overflow: hidden;
  9.   transition:max-height .4s;
  10. }
  11. .container:hover{
  12.   max-height: 111px;
  13. }
  14. .header{
  15.   width: 100%;
  16.   height: 48px;
  17.   background-color: #ccc;
  18.   display: flex;
  19.   align-items: center;
  20.   justify-content: center;
  21. }
  22. .list{
  23.   background-color: green;
  24. }
复制代码
演示地址:https://jsbin.com/cedifocuci/edit?html,css,output
1.gif

注意:
max-height设的太高动画效果会太快,设的太小又无法适配所有高度,此方案只能说是能解决问题,但是不是最理想的方案
通过Grid布局来实现

通过网络布局,一开始设置grid-template-rows为0fr,这里有一些技巧,如果子元素有内容,0fr是不生效的,要给子元素设置min-height为0;如果初始要有基础高度,可以设置min-height为指定高度来实现基础高度,此处为48px
关键代码如下:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.   
  10.    
  11.       header
  12.       <ul >
  13.         <li>Grid111111111</li>
  14.         <li>Grid2222222222</li>
  15.         <li>Grid333333333</li>
  16.         <li>Grid444444444</li>
  17.       </ul>
  18.    
  19.   
  20. </body>
  21. </html>
复制代码
  1. *{
  2.   margin: 0;
  3.   padding: 0;
  4. }
  5. .container{
  6.   width: 100%;
  7.   display: grid;
  8.   grid-template-rows: 0fr;
  9.   min-height: 48px;
  10.   overflow: hidden;
  11.   transition:all .4s;
  12. }
  13. .inner{
  14.   min-height: 0;
  15. }
  16. .container:hover{
  17.   grid-template-rows: 1fr;
  18. }
  19. .header{
  20.   width: 100%;
  21.   height: 48px;
  22.   background-color: #ccc;
  23.   display: flex;
  24.   align-items: center;
  25.   justify-content: center;
  26. }
  27. .list{
  28.   background-color: green;
  29. }
复制代码
演示地址:https://jsbin.com/xasuviputo/edit?html,css,output
2.gif

注意:
主流浏览器都支持,但是部分国内浏览器还是有很大兼容问题,如果对兼容性有很高要求的话此方案就不合适了,同时如果你有基础高度,那基础高度的那一段是没有动画效果的
3.png

通过clip-path来实现

clip-path又叫CSS裁剪路径,clip-path 属性用于创建一个元素的可视区域,区域内的内容会显示,而区域外的内容则会被隐藏。这个属性可以用来对元素进行形状裁剪,实现各种视觉效果
此处使用clip-path:inset()来实现,可以传入四个数字inset(1 2 3 4),它们的规则如下图
4.png

关键代码如下:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.   
  10.    
  11.       header
  12.       <ul >
  13.         <li>clip-path111111111</li>
  14.         <li>clip-path2222222222</li>
  15.         <li>clip-path333333333</li>
  16.         <li>clip-path444444444</li>
  17.       </ul>
  18.    
  19.   
  20. </body>
  21. </html>
复制代码
  1. *{
  2.   margin: 0;
  3.   padding: 0;
  4. }
  5. .container{
  6.   width: 100%;
  7.   clip-path: inset(0 0 calc(100% - 48px) 0);
  8.   overflow: hidden;
  9.   transition:all .4s;
  10. }
  11. .container:hover{
  12.   clip-path: inset(0 0 0 0);
  13. }
  14. .header{
  15.   width: 100%;
  16.   height: 48px;
  17.   background-color: #ccc;
  18.   display: flex;
  19.   align-items: center;
  20.   justify-content: center;
  21. }
  22. .list{
  23.   background-color: green;
  24. }
复制代码
演示地址:https://jsbin.com/zelopuyuxi/edit?html,css,output
5.gif

注意:
主流浏览器都支持,但是部分国内浏览器还是有很大兼容问题,如果对兼容性有很高要求的话此方案就不合适了,相比grid此方案不会有基础高度无过渡效果的问题
6.png

小结

对于做技术的我们,每天都是提出问题解决问题的一个过程,过程中会尝试各种方案,因为解决问题的方案千千万,此篇文章记录了实现不定高内容过渡效果的三种实现方式,怕篇幅太长,此文暂时写这么多,下篇继续……

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册