javascript - transition height auto 過渡動畫
問題描述
1.為什么收縮時,沒有動畫效果?
2.代碼
<!DOCTYPE html><html lang='en'><head> <title></title> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1'> <style>* { transition: all .6s;}.container { position: fixed; top: 0; left: 0; right: 0; height: 100px; max-height: 100px; width: 100px; margin: 5px auto; background: RGBA(0, 43, 54, 0.80); overflow: hidden; text-align: center;}.container:hover { height: auto; max-height: 100%; bottom: 0px;} </style></head><body> <p class='sketch'><p class='container'> <!--<a href='javascript:void(0)'>開關(guān)</a>--></p> </p> <!--<script>const classList = document.querySelector(’.container’).classList;document.querySelector(’#switch’).addEventListener(’click’, function (e) { if (classList.contains(’expand’)) {document.querySelector(’.container’).classList.remove(’expand’); } else {document.querySelector(’.container’).classList.add(’expand’); }}); </script>--></body></html>
3.在線Demo(己解決)
問題解答
回答1:因為我們所能看到的過渡動畫,其實是height值的變化過程,而你在hover屬性中,并沒有給height賦予明確的值,因此在移出鼠標(biāo)之后,瀏覽器其實并不知道該從哪個值變化到初始值,于是就直接返回到初始值,所以沒有過渡效果
回答2:原因如1樓所說。可以設(shè)置height:100%;。
回答3:.container:hover { height: 100%; // 這個要明確值 max-height: 100%; bottom: 0px;}
相關(guān)文章:
1. vue.js - 為什么我的vue項目上傳到github不能預(yù)覽?2. javascript - 函數(shù)聲明和匿名函數(shù)有什么不同?(前端小白求助。。)3. javascript - 求助一個關(guān)于indexedDB的問題4. mysql - linux ubuntu14.04找不到3306端口5. css3 - border-bottom 的長度可否超過盒子的寬度呢?實現(xiàn)如下圖效果。(我的書下面的線)6. html5 - 如何實現(xiàn)圖中的刻度漸變效果?7. 淺談vue生命周期共有幾個階段?分別是什么?8. index.php錯誤,求指點9. java - web端百度網(wǎng)盤的一個操作為什么要分兩次請求服務(wù)器, 有什么好處嗎10. css - 移動端 oppo 手機(jī)之 Border-radius
