- <!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