更少的自定义函数/mixin

标签 less less-mixins

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

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

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

    }
}  

我想写一些类似的东西

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

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

最佳答案

我猜所有这些例子都在 the docs可以提出一些想法:

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}
        }
    }
}

关于更少的自定义函数/mixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024800/

相关文章:

css - Less mixin 中带参数的动态类名

css - 在 LESS 中为基于基色的颜色变体创建变量列表

css - Less::错误/无法识别的输入 - twitter-bootstrap-rails

css - 使用 LESS 自定义样式

html - 如何避免重复多个 css 类

css - 您如何编写供多个类使用的 LESS 规则?

css - 元素动画关键帧的继承

twitter-bootstrap - Twitter bootstrap - 如何删除子类中的渐变混合

less - 合并较少 mixin 输出