【问题标题】:Less custom function/mixin更少的自定义函数/mixin
【发布时间】:2014-06-03 20:58:29
【问题描述】:

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

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

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

    }
}  

我想写一些类似的东西

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

为了得到同样的结果。我该怎么做?

【问题讨论】:

    标签: less less-mixins


    【解决方案1】:

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

    【讨论】:

    • 抱歉耽搁了。没有机会尝试。这真的很有效,非常感谢。隐藏它的结构知识真是太好了。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2014-10-27
    相关资源
    最近更新 更多