transition和height同時使用不生效問題

CSS 支持動畫的屬性中的 height 屬性如下:

height :yes, as a length, percentage or calc()

即:當(dāng) height 的值是 length,百分比或 calc() 時支持 CSS3 過渡。

所以當(dāng)元素 height : auto 時,是不支持 CSS3 動畫的。

對于動態(tài)內(nèi)容,元素的高度應(yīng)設(shè)置為 auto。這樣,任何增加或減少的元素高度都將自適應(yīng)。但也會出現(xiàn)另一個問題:當(dāng)元素的高度設(shè)置為 auto 時,CSS transition 將不起作用。
1、確定高度的時候:
改為max-height
2、不確定高度需要使用js來實現(xiàn)

<template>
  <div>
    <div>this is a vue file called ulTransition.vue;</div>
    <hr />
    <div class="flex-div">
      <div class="div">
        <ul ref="ul" class="showall:true">
          <li v-for="i in testnum" :key="i">測試數(shù)據(jù){{ i }}</li>
        </ul>
      </div>
      <div>
        <button @click="toggle">收縮/放出</button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  namespace: 'ulTransition',
  data () {
    return {
      showli: false,
      testnum: 0,
      ulheight: 0
    }
  },

  mounted () {
    this.testnum = Math.ceil(Math.random() * 10 + 10)
    console.log(this.testnum)

    this.$nextTick((e) => {
      console.log('開始了!')
      this.ulheight = this.$refs.ul.offsetHeight + 'px'
      this.$refs.ul.style.height = this.ulheight
    })
  },
  methods: {
    toggle () {
      if (this.showli) {
        this.$refs.ul.style.height = this.ulheight
      } else {
        this.$refs.ul.style.height = 0
      }
      this.showli = !this.showli
    }
  }
}
</script>
<style scoped>
.flex-div {
  width: 100%;
  display: flex;
}
.flex-div .div {
  flex: 1;
  text-align: center;
}
li {
  width: 100px;
  background-color: #999;
  margin-top: 1px;
}
ul {
  background-color: #f2f2f2;
  transition: all 0.5s;
  overflow: hidden;
}
</style>

https://blog.csdn.net/HUSHILIN001/article/details/94400705?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-94400705-blog-98843611.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-94400705-blog-98843611.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=7

https://blog.csdn.net/echo008/article/details/123993118

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、多樣本讀取與創(chuàng)建對象 參考:Seurat包之導(dǎo)入單細(xì)胞數(shù)據(jù)方式匯總[https://www.jianshu.c...
    oceanandshore閱讀 859評論 0 6
  • paddle edu[https://paddlepedia.readthedocs.io/en/latest/t...
    IIS_Chaser閱讀 360評論 1 2
  • 1、該軟件的使用需要在java環(huán)境內(nèi),下載java并安裝 https://download.oracle.com/...
    luly閱讀 3,653評論 1 3
  • 什么是索引?索引的種類? 索引是幫助數(shù)據(jù)庫高效獲取數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),是基于數(shù)據(jù)庫表創(chuàng)建的,包含一個表中某些列的值以及...
    西谷haul閱讀 364評論 0 0
  • 2022-05-03 首先添加ESP8266板級支持進入首選項(Preferences),找到附加開發(fā)板管理器地址...
    思求彼得趙閱讀 641評論 0 0

友情鏈接更多精彩內(nèi)容