Generate styles using LESS recursive function and

2019-02-26 13:04发布

问题:

I am trying to generate some relative to screen height and decided to try to use LESS to generate something like that, even if a bit heavy just as a test:

@baseHeight: 1px;
.setRelativeHeight(@screenHeight, @minHeightDiff, @maxHeightDiff) when(@screenHeight < 2400) {

    @media (min-height: @baseHeight * @screenHeight) {
        min-height: @baseHeight * (@screenHeight - @minHeightDiff);
        max-height: @baseHeight * (@screenHeight - @maxHeightDiff);
    }
    .setRelativeHeight(@screenHeight + 20, @minHeightDiff, @maxHeightDiff);
}

That seems to work, most of it, but this is part of what it generates when calling it:

@media not all {
#ConversationMessages .messages {
    max-height: 2100px;
    min-height: 2000px;
}
}
@media not all {
#ConversationMessages .messages {
    max-height: 2120px;
    min-height: 2020px;
}
}
@media not all {
#ConversationMessages .messages {
    max-height: 2140px;
    min-height: 2040px;
}
}
@media not all {
#ConversationMessages .messages {
    max-height: 2160px;
    min-height: 2060px;
}
}

So the styles are being set properly but the media condition is lost :( Does anyone know why? :)

Thanks!!

Update Fixed adding parenthesis to the media condition (see comment below).

回答1:

Arithmetic operations inside @media queries should always be in parens regardless of --strict-math option. I.e. should be @media (min-height: (@baseHeight * @screenHeight)).



标签: css less