1、在template中:
<div class="link-list">
<ul>
<li
class="link-item"
:class="{'link-item-active': item.id == activeLinkId,'link-item-hover':index==hoverIndex}"
v-for="(item,index) in linkGroupData"
:key="index"
@click="switchActiveLink(item)"
@mouseover="hoverIndex = index"
@mouseout="hoverIndex = -1">
{{item.name}}
</li>
</ul>
</div>
tips多个class类名,可用连写在{}中,逗号隔开;
2、在data中定义两个索引值:
hoverIndex: -1, // 菜单hover索引
activeLinkId: 0, // 当前激活的菜单id
3、在css中写两个样式:
.link-item-hover {
background-color: #f5f7fa;
}
.link-item-active {
// 链接菜单激活样式
color: #fff;
background-color: #b3cae4;
}
参考文章:
https://www.jianshu.com/p/6f841d5a6090