设置背景色的一种方法
active
hover
每个按钮的状态不同
:nth-child
CSS伪类:
.subNav > .nav-item:nth-child(1) > .nav-link:hover,
.subNav > .nav-item:nth-child(1) > .nav-link.active {
background-color: orange;
}
.subNav > .nav-item:nth-child(2) > .nav-link:hover,
.subNav > .nav-item:nth-child(2) > .nav-link.active {
background-color: green;
}
.subNav > .nav-item:nth-child(3) > .nav-link:hover,
.subNav > .nav-item:nth-child(3) > .nav-link.active {
background-color: red;
}
.subNav > .nav-item:nth-child(4) > .nav-link:hover,
.subNav > .nav-item:nth-child(4) > .nav-link.active {
background-color: blue;
}
看见
this stackblitz
演示一下。
另一种方法是在数据结构中定义活动状态颜色:
this.lineas = [
{ image: 'assets/images/icon1.svg', url:'url1', titulo: 'title1', activeColor: 'lime' },
{ image: 'assets/images/icon2.svg', url:'url2', titulo: 'title2', activeColor: 'green' },
{ image: 'assets/images/icon3.svg', url:'url3', titulo: 'title3', activeColor: 'red' },
{ image: 'assets/images/icon4.svg', url:'url4', titulo: 'title4', activeColor: 'blue'}
];
并将其应用于链接的背景样式时
积极的
已设置类:
<a #link [style.background-color]="isActive(link) ? linea.activeColor : null" ...>
isActive(link): boolean {
return link.classList.contains("active");
}
看见
this stackblitz
演示一下。您会注意到我为
悬停
在这种情况下说明:
.subNav > .nav-item > .nav-link:hover {
background-color: #c0c0c0;
}
您可能需要处理鼠标
enter
和
leave
如果要在鼠标悬停在链接上时应用数据中定义的自定义背景色,则会发生事件。