首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >较少的组织:nth-子活动状态

较少的组织:nth-子活动状态
EN

Stack Overflow用户
提问于 2015-03-04 18:53:58
回答 1查看 1.2K关注 0票数 0

我在这个项目中使用了less,目前正在设计一个导航栏,并使用:nth-childli元素的样式设置为3的倍数。我还试图管理active状态(如下面的//li active states for nav注释所示)。

我试图使任何active li项目都具有background-color: white。下面的解决方案补充如下:

代码语言:javascript
复制
            &:nth-child(3n + 1):hover {
              background-color: white;
            }

到每个active :nth-child声明。当然,有一种方法可以做一些类似于&:nth-child(all):hover或DRYer的事情,而不是我下面的内容。看我的更少:

代码语言:javascript
复制
       li {
          color: white;
          padding: 0.9em;
          // nav item 1 and multiple
          &:nth-child(3n + 1) {
            border-top: 2px solid @blue;
            &:nth-child(3n + 1):hover {
              background-color: @blue;
            }
          }
          // nav item 2 and multiple
          &:nth-child(3n + 2) {
            border-top: 2px solid @red;
            &:nth-child(3n + 2):hover {
              background-color: @red;
            }
          }
          // nav item 3 and multiple
          &:nth-child(3n + 3) {
            border-top: 2px solid @green;
            &:nth-child(3n + 3):hover {
              background-color: @green;
            }
          }
        }
        // li active states for Nav
        .active {
          background-color: white;
          &:nth-child(3n + 1) {
            color: @blue;
            &:nth-child(3n + 1):hover {
              background-color: white;
            }
          }
          &:nth-child(3n + 2) {
            color: @red;
            &:nth-child(3n + 2):hover {
              background-color: white;
            }
          }
          &:nth-child(3n + 3) {
            color: @green;
            &:nth-child(3n + 3):hover {
              background-color: white;
            }
          }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-04 18:59:30

你应该改变这个..。

代码语言:javascript
复制
  &:nth-child(3n + 1) {
    border-top: 2px solid @blue;
    &:nth-child(3n + 1):hover {
      background-color: @blue;
    }
  }

..。为了这个..。

代码语言:javascript
复制
  &:nth-child(3n + 1) {
    border-top: 2px solid @blue;
    &:hover {
      background-color: @blue;
    }
  }

你的少意志输出..。

代码语言:javascript
复制
li:nth-child(3n + 1):nth-child(3n + 1):hover

..。但你想..。

代码语言:javascript
复制
li:nth-child(3n + 1):hover

遵循这一模式,贯穿所有其他的你的更少。

至于.active状态- li.active将具有与li:nth-child(3n + 1)等相同的特异性,因此只需在:n个选择器之后包含li.active

//编辑-最终解决方案

代码语言:javascript
复制
li {
    color: white;
    padding: 0.9em;
    // nav item 1 and multiple
    &:nth-child(3n + 1) {
        border-top: 2px solid @blue;
        &:hover {
            background-color: @blue;
        }
        &.active{
            color: @blue;
        }
    }
    // nav item 2 and multiple
    &:nth-child(3n + 2) {
        border-top: 2px solid @red;
        &:hover {
            background-color: @red;
        }
        &.active{
            color: @red;
        }
    }
    // nav item 3 and multiple
    &:nth-child(3n + 3) {
        border-top: 2px solid @green;
        &:hover {
            background-color: @green;
        }
        &.active{
            color: @green;
        }
    }
    // li active states for Nav
    &.active{
        background-color: white;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28862542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档