CSS3 transition使用max-height收缩时卡顿的原因

本文详细解释了CSS3过渡动画在使用max-height属性时可能出现的卡顿现象。当内容高度未达到max-height设定值时,元素会从较高的max-height开始收缩,导致视觉上的延迟,从而产生卡顿感。了解这一机制有助于优化网页动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSS3过渡动画transition,使用max-height时会有像卡一下才进行收缩的原因,因为收缩动画是以max-height高度来算的,如果你的实际内容高度没有达到max-height高度,它收缩时是从比实际高度要高的max-height定义的高度的地方开始往下收所以要等高度收到和实际高度差不多时,你才能看见收缩效果,于是给你造成卡一下的感觉。

<script setup lang="ts"> import { ref } from &#39;vue&#39; const searchText = ref(&#39;&#39;) const isSearching = ref(false) const results = ref<string[]>([]) const handleSearch = () => { if (!searchText.value) { isSearching.value = false return } else { isSearching.value = true setTimeout(() => { results.value = Array.from({ length: 10 }, (_, i) => `结果 ${i + 1}: ${searchText.value}`) }, 500) } } </script> <template> <div class="container" :class="{ &#39;search-active&#39;: isSearching }"> <div class="search-box"> <input v-model="searchText" type="text" placeholder="输入搜索内容" @keyup.enter="handleSearch"> <button @click="handleSearch"> 搜索 </button> </div> <transition name="list"> <ul v-if="isSearching" class="result-list"> <li v-for="(item, index) in results" :key="index"> {{ item }} </li> </ul> </transition> </div> </template> <style> .container { height: 100vh; display: flex; flex-direction: column; align-items: center; transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1); } .container:not(.search-active) { justify-content: center; } .search-active { padding-top: 50px; } .search-box { display: flex; transition: transform 0.3s ease; } input { width: 300px; padding: 12px 20px; border-radius: 25px; font-size: 16px; } button { padding: 12px 25px; background: #42b883; border-radius: 25px; color: white; cursor: pointer; } .result-list { width: 500px; max-height: 60vh; overflow-y: auto; margin-top: 30px; padding: 0; list-style: none; } /* 过渡动画 */ .list-enter-active, .list-leave-active { transition: all 0.5s ease; } .list-enter-from, .list-leave-to { opacity: 0; transform: translateY(-20px); } </style> 现在过度动画很不流畅尤其是搜索框回到初始状态的
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值