css動畫效果:實現鼠標移入菜單欄文字下出現下劃線
阿新 • • 發佈:2018-01-06
boa 代碼 active lock osi int 事件 type 添加
- <!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>
廢話不多說先上個效果吧:效果演示
其實是個超級簡單的動畫,不過看到現在很多的網站在處理菜單欄的時候,一般都是用鼠標移入背景顏色變化或者字體顏色變化來告訴用戶他即將訪問的頁面和當前所在的頁面,我自己感覺這個小動畫在這裏比起那種效果要好看很多,所以也算替自己總結吧,就寫下來了。
要用一個比較重要的選擇器 :before選擇器
w3cschool是這麽說的:before 偽元素可以在元素的內容前面插入新內容。
首先寫html代碼:
[html] view plain copy- <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表示當前頁面的樣式。
[html] view plain copy
- .nav-underline-active,
- .nav-underline-active:hover {
- border-bottom: 2px solid #fff;
- text-align: center;
- }
元素的定位很重要,將文字定位為relative,而:before定位為absolute
- 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
[html] view plain copy
- header nav a{
- position: relative;
- text-decoration: none;
- color: #fff;
- display: block;
- padding: 1.2em 0.8em;
- }
然後可以定義動畫了,大家應該註意到hover事件下劃線的width由原來的0變為80%,其實動畫效果也就是改變它的寬度值,給寬度變化增加過渡效果
[html] view plain copy
- 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;
- }
簡單的動畫已經完成啦,我把完整的代碼貼出來吧:
css動畫效果:實現鼠標移入菜單欄文字下出現下劃線