- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>菜單欄下劃線動畫</title>
- <style type="text/css">
- body{
- margin: 0;
- padding: 0;
- }
- header{
- width: 100%;
- height: 100px;
- background-color:#2D3E50;
- }
- header nav ul{
- width: 50%;
- padding: 0;
- margin: 0 auto;
- }
- header nav ul li{
- display: inline-block;
- padding: 0 0.8em;
- }
- header nav a{
- position: relative;
- text-decoration: none;
- color: #fff;
- display: block;
- padding: 1.2em 0.8em;
- }
- header nav .nav-underline:before {
- content: "";
- position: absolute;
- bottom: 0;
- width: 0;
- border-bottom: 2px solid #fff;
- }
- header nav .nav-underline:hover:before {
- width: 80%;
- }
- header nav .nav-underline:before {
- -webkit-transition: width 0.5s ease-in-out;
- -moz-transition: width 0.5s ease-in-out;
- -ms-transition: width 0.5s ease-in-out;
- -o-transition: width 0.5s ease-in-out;
- transition: width 0.5s ease-in-out;
- }
- header nav .nav-underline-active,
- header nav .nav-underline-active:hover {
- border-bottom: 2px solid #fff;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <header>
- <nav>
- <ul>
- <li class=" pure-menu-selected"><a href="#" class="nav-underline-active">HOME</a></li>
- <li ><a href="#" class="nav-underline">SKILLS</a></li>
- <li ><a href="#" class="nav-underline">INTERESTS</a></li>
- <li ><a href="#" class="nav-underline">CONTACT ME</a></li>
- </ul>
- </nav>
- </header>
- </body>
- </html>
廢話很少說先上個效果吧:效果演示css
實際上是個超級簡單的動畫,不過看到如今不少的網站在處理菜單欄的時候,通常都是用鼠標移入背景顏色變化或者字體顏色變化來告訴用戶他即將訪問的頁面和當前所在的頁面,我本身感受這個小動畫在這裏比起那種效果要好看不少,因此也算替本身總結吧,就寫下來了。html
要用一個比較重要的選擇器 :before選擇器web
w3cschool是這麼說的:before 僞元素能夠在元素的內容前面插入新內容。字體
首先寫html代碼:動畫
- <ul>
- <li class=" pure-menu-selected"><a href="#" class="nav-underline-active">HOME</a></li>
- <li ><a href="#" class="nav-underline">SKILLS</a></li>
- <li ><a href="#" class="nav-underline">INTERESTS</a></li>
- <li ><a href="#" class="nav-underline">CONTACT ME</a></li>
- </ul>
爲類名爲nav-underline的a元素添加動畫效果,類名爲nav-underline-active表示當前頁面的樣式。網站
- .nav-underline-active,
- .nav-underline-active:hover {
- border-bottom: 2px solid #fff;
- text-align: center;
- }
元素的定位很重要,將文字定位爲relative,而:before定位爲absolutespa
- header nav .nav-underline {
- position: relative;
- text-decoration: none;
- color: #fff;
- display: block;
- }
- header nav .nav-underline:before {
- content: "";
- position: absolute;
- bottom: 0;
- width: 0;
- border-bottom: 2px solid #fff;
- }
- header nav .nav-underline:hover:before {
- width: 80%;
- }
a元素必定要設置爲display:block.net
- header nav a{
- position: relative;
- text-decoration: none;
- color: #fff;
- display: block;
- padding: 1.2em 0.8em;
- }
而後能夠定義動畫了,你們應該注意到hover事件下劃線的width由原來的0變爲80%,其實動畫效果也就是改變它的寬度值,給寬度變化增長過渡效果xml
- header nav .nav-underline:before {
- -webkit-transition: width 0.5s ease-in-out;
- -moz-transition: width 0.5s ease-in-out;
- -ms-transition: width 0.5s ease-in-out;
- -o-transition: width 0.5s ease-in-out;
- transition: width 0.5s ease-in-out;
- }
簡單的動畫已經完成啦,我把完整的代碼貼出來吧:htm