在製做頂部菜單的時候,都會要求製做彈出的二級菜單,早先的作法是用jQuery的來控制二級菜單的顯示和過渡動畫,但利用CSS3中的transform屬性後,這一切都變得異常簡單php
先上效果
css
核心就是利用了transform的區域位移方法,在配合上li標籤的hover僞類和動畫延時,從而簡單實現了子菜單的顯示html
<nav> <ul> <li> <strong>home</strong> <div> <a href="">cms</a> <a href="">crm</a> </div> </li> <li> <strong>live</strong> <div> <a href="">java</a> <a href="">php</a> </div> </li> <li> <strong>pictrue</strong> <div> <a href="">mm</a> <a href="">dd</a> </div> </li> </ul> </nav>
*{ padding: 0; margin: 0; box-sizing: border-box; } body{ width: 100vw; height: 100vh; display: flex; flex-direction: column; justify-content: flex-start; align-items: center; } nav{ margin: 10px; } nav ul { list-style-type: none; height: 32px; display: flex; } nav ul li{ margin-right: 10px; } nav ul li strong{ text-transform: uppercase; background-color: #9b59b6; color: white; padding: 5px 30px; line-height: 30px; cursor: pointer; } nav ul li strong+div{ display: flex; flex-direction: column; background-color: #3498db; padding: 10px; transform: translateY(-110%); opacity: 0; transition: .3s; transform-origin: top; } nav ul li:hover div{ transform: translateY(0); opacity: 1; } nav ul li strong+div a{ color: white; text-decoration: none; text-transform: uppercase; padding: 5px 0; }