css动画效果:实现鼠标移入菜单栏文字下出现下划线

<!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代码:

<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定位为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

header nav a{
		  position: relative;
		  text-decoration: none;
		  color: #fff;
		  display: block;
		  padding: 1.2em 0.8em;
		}
然后可以定义动画了,大家应该注意到hover事件下划线的width由原来的0变为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;
        }
简单的动画已经完成啦,我把完整的代码贴出来吧:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值