Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

targeting dom elements with data attributes with a value of (n) #1267

Closed
heaversm opened this issue Apr 11, 2013 · 2 comments
Closed

targeting dom elements with data attributes with a value of (n) #1267

heaversm opened this issue Apr 11, 2013 · 2 comments

Comments

@heaversm
Copy link

I have a navigational menu which assigns an array of colors to menu items based on their depth within a menu hierarchy / taxonomy. So, for example, all top level menu items get a color of black, next level gets red, next level gets green, etc, and because the hierarchy goes very deep, I'd like to target them using math, sort of the way that css can target nth-child. However, I can't use nth-child because these container elements ('browse-level') are dynamically added and removed from the DOM (they're not all in the DOM at the same time) which is why I'm target data attributes.

So here's the CSS:

    .browse-level[data-level="1"] li a {
      background: @level1;
    }

    .browse-level[data-level="2"] li a {
      background: @level2;
    }

    .browse-level[data-level="3"] li a {
      background: @level3;
    }

    .browse-level[data-level="4"] li a {
      background: @level4;
    }

... etc

There are 8 color values (after which the sequence would repeat). Can I (using LESS or plain CSS) shorten this code?

@lukeapage
Copy link
Member

in 1.4.0

.browse-mixin(@level) when (@level > 0) {
    @attr: ~'data-level="@{level}"';
    .browse-level[@{attr}] li a {
         @varname: "level@{level}";
         background: @@varname;
    }
    .browse-mixin((@level - 1));
}
.browse-mixin(4);
@level1: a;
@level2: b;
@level3: c;
@level4: d;

in 1.3.3

.browse-mixin(@level) when (@level > 0) {
    @attr: ~'.browse-level[data-level="@{level}"]';
    @{attr} li a {
         @varname: "level@{level}";
         background: @@varname;
    }
    .browse-mixin((@level - 1));
}
.browse-mixin(4);
@level1: a;
@level2: b;
@level3: c;
@level4: d;

??

@jonschlinkert
Copy link
Contributor

this is a good question for stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants