制作网页的时候,我们会想实现这样的效果:把鼠标移动到导航栏上时出现下划线。
接下来我们就来实现一下导航栏下划线跟随滑动效果,代码如下:
1.下划线从右往左滑动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>css导航栏下划线效果</title>
<style>
ul {
display: flex;
list-style:none
}
li {
padding: 20px;
font-size: 24px;
color: #000;
position: relative;
transition: 0.1s all linear;
cursor: pointer;
}
li:before {
content: "";
position: absolute;
top: 0;
left: 100%;
width: 0;
height: 100%;
border-bottom: 2px solid #000;
transition: 0.2s all linear;
}
li:hover:before {
width: 100%;
top: 0;
left: 0;
transition-delay: 0.1s;
border-bottom-color: #000;
}
li:hover ~ li:before {
left: 0;
}
</style>
</head>
<body>
<div>
<ul>
<li>首页</li>
<li>关于我们</li>
<li>产品中心</li>
<li>案例展示</li>
<li>联系我们</li>
</ul>
</div>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>css导航栏下划线效果</title>
<style>
ul {
display: flex;
list-style:none
}
li {
padding: 20px;
font-size: 24px;
color: #000;
position: relative;
transition: 0.1s all linear;
cursor: pointer;
}
li:before {
content: "";
position: absolute;
top: 0;
left: 100%;
width: 0;
height: 100%;
border-bottom: 2px solid #000;
transition: 0.2s all linear;
}
li:hover:before {
width: 100%;
top: 0;
left: 0;
transition-delay: 0.1s;
border-bottom-color: #000;
}
li:hover ~ li:before {
left: 0;
}
</style>
</head>
<body>
<div>
<ul>
<li>首页</li>
<li>关于我们</li>
<li>产品中心</li>
<li>案例展示</li>
<li>联系我们</li>
</ul>
</div>
</body>
</html>
2.下划线从左往右滑动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>css导航栏下划线效果</title>
<style>
ul {
display: flex;
list-style:none
}
li {
padding: 20px;
font-size: 24px;
color: #000;
position: relative;
transition: 0.1s all linear;
cursor: pointer;
}
li:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 0;
height: 100%;
border-bottom: 2px solid #000;
transition: 0.2s all linear;
}
li:hover:before {
width: 100%;
top: 0;
left: 0;
transition-delay: 0.1s;
border-bottom-color: #000;
}
li:hover ~ li:before {
left: 0;
}
</style>
</head>
<body>
<div>
<ul>
<li>首页</li>
<li>关于我们</li>
<li>产品中心</li>
<li>案例展示</li>
<li>联系我们</li>
</ul>
</div>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>css导航栏下划线效果</title>
<style>
ul {
display: flex;
list-style:none
}
li {
padding: 20px;
font-size: 24px;
color: #000;
position: relative;
transition: 0.1s all linear;
cursor: pointer;
}
li:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 0;
height: 100%;
border-bottom: 2px solid #000;
transition: 0.2s all linear;
}
li:hover:before {
width: 100%;
top: 0;
left: 0;
transition-delay: 0.1s;
border-bottom-color: #000;
}
li:hover ~ li:before {
left: 0;
}
</style>
</head>
<body>
<div>
<ul>
<li>首页</li>
<li>关于我们</li>
<li>产品中心</li>
<li>案例展示</li>
<li>联系我们</li>
</ul>
</div>
</body>
</html>