简体   繁体   English

用于无cookie域的自定义javascript函数的CSS少

[英]LESS CSS with custom javascript function for cookieless domain

I'd like to use less ( http://lesscss.org/ ) instead of sass ( http://sass-lang.com/ ) for preprocessing css. 我想使用less( http://lesscss.org/ )而不是sass( http://sass-lang.com/ )来预处理css。 I have a set of cookieless domains for static resources. 我有一组用于静态资源的无cookie域。 For example: 0.mydomain.com, 1.mydomain.com, 2.mydomain.com, etc. I would like to compile CSS using less such that the cookieless domains are injected into the compiled CSS output. 例如:0.mydomain.com,1.mydomain.com,2.mydomain.com等。我想使用less编译CSS,以便将无cookie域注入编译的CSS输出中。 I found this ability to to create custom functions in the sass docs using @function. 我发现这种能力使用@function在sass文档中创建自定义函数。 Does the equivalent exist for less (I can not find)? 等价物是否存在较少(我找不到)? I need a function that performs a hashing algorithm to convert a filename into a number X corresponding to a cookieless domain (X.mydomain.com). 我需要一个执行散列算法的函数将文件名转换为对应于无Cookie域(X.mydomain.com)的数字X. How would one do this using less? 怎么会少用这个呢?

The below example is contrived for illustration: 下面的例子是为了说明而设计的:

In my.less file: 在my.less文件中:

@function domainX(path) {
    //configs
    var protocol = "http://";
    var domain = ".mydomain.com"
    var N = 4; //4 cookieless domains

    var sum = 0;
    var s = path.substr(path.lastIndexOf("/") + 1);
    for (var i = 0; i < s.length; i++) {
        sum += s[i].charCodeAt();
    }
    @return protocol + (sum % N) + domain + path;
}

.myItem {background-image:url(domainX('/images/background.jpg')) }

that would generate compiled output 这将生成编译输出

.myItem {background-image:url('http://1.mydomain.com/images/background.jpg') }

The SASS example is http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#functions SASS示例是http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#functions

See section "Function Directives" 请参阅“功能指令”部分

The closest example from the LESS docs is below, but there's no function to construct the base-url. 最接近LESS文档的示例如下,但是没有构造base-url的功能。

@base-url: "http://assets.fnord.com";
background-image: url("@{base-url}/images/bg.png");

Maybe there is a LESS + Node.js part of the solution too? 也许解决方案中还有一个LESS + Node.js部分?

Thanks! 谢谢!

You should be able to do this using LeSS' ability to embed js: 您应该能够使用LeSS的嵌入js的能力来做到这一点:

.background(@path) {
    background-image: ~`(function(){ var protocol = "http://"; var domain = ".mydomain.com"; var N = 4; var sum = 0; var s = @{path}.substr(@{path}.lastIndexOf("/") + 1); for (var i = 0; i < s.length; i++) { sum += s[i].charCodeAt(); } return "url(" + protocol + (sum % N) + domain + @{path} + ")";})()`;
}

.myItem {
    .background("/images/background.jpg");
}

no idea what the performance would be like, but then if you're processing server side you won't care, and client side it caches so shouldn't be a problem. 不知道性能会是什么样的,但是如果你正在处理服务器端你不会关心,而客户端它的缓存所以不应该是一个问题。

No. LESS has considerably less features than Sass (no functions, no loops). 没有.LESS的功能比Sass少得多(没有功能,没有循环)。 You would have to use a mixin to do anything remotely like that. 你必须使用mixin来做任何远程的事情。 Sass could do it except for the fact that it doesn't have any string manipulation functions built in, so you'd have to write a bit of Ruby code to add those in. Sass可以做到这一点,除了它没有内置的任何字符串操作函数,所以你必须编写一些Ruby代码来添加它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM