2

例如我有下一个更少的代码

form.someForm
{
    .form-group
    {
        > div
        {
            @media @wideMobile
            {
                width: calc(~"100%-144px");            
            }
        }

        > label
        {
            @media @wideMobile
            {
                width: 138px;            
            }
        }

    }
}  

我想写类似的东西

form.someForm
{
    .setWidths(138px,144px); 
}

得到同样的结果。我怎样才能做到这一点?

4

1 回答 1

7

我想文档中的所有这些示例都可以产生一些想法:

form.someForm {
    .setWidths(138px, 144px);
}

// the mixin:

.setWidths(@labelWidth, @divMargin) {
    .form-group {
        > div {
            @media @wideMobile {
                width: calc(100% ~'-' @divMargin);
            }
        }

        > label {
            @media @wideMobile {
                width: @labelWidth;
            }
        }
    }
}

甚至更短(如果后代共享相同的媒体查询):

.setWidths(@labelWidth, @divMargin) {
    .form-group {
        @media @wideMobile {
            > div   {width: calc(100% ~'-' @divMargin)}
            > label {width: @labelWidth}
        }
    }
}
于 2014-06-03T21:58:00.337 回答