最终实现样式:
通过一组ul,li标签实现导航栏
<div class="sear-list">
<ul>
<li class="current"><span>全部搜索</span></li>
<li><span>图书</span></li>
<li><span>期刊论文</span></li>
<li><span>学位论文</span></li>
<li><span>标准</span></li>
<li><span>纸本馆藏</span></li>
<li><span>外文资源发现</span></li>
<li><span>数据库导航</span></li>
</ul>
</div>
对应样式为:
/* reset */
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
ul,li{
list-style: none;
}
/* 导航栏样式 -start */
.sear-list{
width: 1000px;
height: 60px;
background-color: #ccc;
padding: 20px 0 0 10px;
margin: 0 auto;
}
/*清除浮动*/
.sear-list ul::after{
content: ‘‘;
display: block;
clear: both;
}
.sear-list ul li{
min-width: 55px;
float: left;
padding: 0 10px;
margin: 0 11px;
line-height: 30px;
height: 30px;
background-color: #fff;
text-align: center;
position: relative;
cursor: pointer;
}
/* 通过伪元素添加添加三角形 */
.sear-list ul li::before{
content: ‘‘;
display: inline-block;
border-width: 0 0 30px 20px;
border-color: #fff;
border-left-color: transparent !important;
border-style: solid;
position: absolute;
left: -20px;
}
.sear-list ul li::after{
content: ‘‘;
display: inline-block;
border-width: 30px 20px 0 0;
border-color: #fff;
border-right-color: transparent !important;
border-style: solid;
position: absolute;
right: -20px;
}
.current{
background-color: red !important;
color: #fff;
}
.current::before,.current::after{
border-color: red !important;
}
/* 下方三角形 */
.current span::after{
content: ‘‘;
display: inline-block;
border-top: 6px solid red;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
position: absolute;
bottom: -6px;
left: 50%;
transform: translateX(-6px);
}
通过jQuery实现导航栏的点击效果,首先需要引入jQuery库:
$(document).ready(function (){// ready 事件
$(".sear-list ul li").each(function(index){//遍历对象
$(this).click(function(){//点击触发事件
$(".sear-list ul li").removeClass("current");//删除当前元素的样式
$(".sear-list ul li").eq(index).addClass("current");//添加当前元素的样式
});
});
});
原文:https://www.cnblogs.com/cianbei/p/15268161.html