在less.css中创建和使用用户函数

Create and use user functions in lesscss

本文关键字:用户 函数 创建 less css      更新时间:2023-09-26

我的html:中有这个

<script type="text/javascript">
    less = {
        env: "development", // or "production"
        async: false,       // load imports async
        fileAsync: false,   // load imports async when in a page under
                            // a file protocol
        poll: 1000,         // when in watch mode, time in ms between polls
        functions: {
            returnCenter: function (value, context) {
                return 'center';
            }
        },      // user functions, keyed by name
        dumpLineNumbers: "comments", // or "mediaQuery" or "all"
        relativeUrls: false,// whether to adjust url's to be relative
                            // if false, url's are already relative to the
                            // entry less file
        rootpath: ":/a.com/"// a path to add on to the start of every url
                            //resource
    };
</script>
<script src="less.js" type="text/javascript"></script>

那么在函数中,如何在less文件中创建和使用函数呢?

谢谢,祝你好运!

p.D.这个回收中心不起作用。带参数或不带参数。

出于某种原因,您应该用小写字母写函数名。您的函数也应该返回正确的类型。

尝试:

<script>
less = { 
    env: "development",
    functions: { 
        returncenter: function(context, value) {
            return new(less.tree.Anonymous)('center');
        }
    }
};
</script>

现在你可以使用以下较少的代码:

selector {
property: returncenter();    
}

前面的Less代码将编译成以下CSS代码:

selector {
property: center;    
}

另请注意:LessCSS的用户定义函数?