CSS下拉動畫是一種常見的網頁效果,它可以使用戶點擊下拉菜單時看到平滑而美觀的動畫效果,增強用戶體驗。
代碼實現如下: /* 下拉菜單的CSS */ .dropdown { position: relative; display: inline-block; } /* 下拉內容的CSS */ .dropdown-content { display: none; position: absolute; z-index: 1; } /* 當鼠標懸停在下拉菜單時,下拉內容顯示 */ .dropdown:hover .dropdown-content { display: block; } /* 下拉內容出現時的動畫效果 */ .dropdown-content { -webkit-animation: fadein 0.5s ease-in-out; -moz-animation: fadein 0.5s ease-in-out; -ms-animation: fadein 0.5s ease-in-out; -o-animation: fadein 0.5s ease-in-out; animation: fadein 0.5s ease-in-out; } /* 下拉內容消失時的動畫效果 */ .dropdown:hover .dropdown-content { -webkit-animation: fadeout 0.5s ease-in-out; -moz-animation: fadeout 0.5s ease-in-out; -ms-animation: fadeout 0.5s ease-in-out; -o-animation: fadeout 0.5s ease-in-out; animation: fadeout 0.5s ease-in-out; } /* 定義動畫的CSS */ @-webkit-keyframes fadein { from { opacity: 0; } to { opacity: 1; } } @-moz-keyframes fadein { from { opacity: 0; } to { opacity: 1; } } @-ms-keyframes fadein { from { opacity: 0; } to { opacity: 1; } } @-o-keyframes fadein { from { opacity: 0; } to { opacity: 1; } } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } } @-webkit-keyframes fadeout { from { opacity: 1; } to { opacity: 0; } } @-moz-keyframes fadeout { from { opacity: 1; } to { opacity: 0; } } @-ms-keyframes fadeout { from { opacity: 1; } to { opacity: 0; } } @-o-keyframes fadeout { from { opacity: 1; } to { opacity: 0; } } @keyframes fadeout { from { opacity: 1; } to { opacity: 0; } }
上述代碼中,我們使用了hover偽類控制下拉菜單內容的顯示和隱藏。動畫效果則是通過CSS3中的@keyframes實現的,用戶可以通過更改動畫的時間、緩動函數、關鍵幀等參數來實現各種不同的動畫效果。