我什至不确定如何问这个问题。 LESS CSS Framework 包含几个操作颜色的函数,我想知道如何自己调用这些函数来修改颜色。问题是这些函数位于另一个函数内并定义如下:
(function (tree) {
tree.functions = {
darken: function(color, amount){
...stuff...
}
}
}
我知道的足以假设 darken 是 tree.functions 的一个方法,但我一直不知道如何调用它,因为它在内部匿名函数(function (tree)。
[编辑] 从@pradeek 获得解决方案后,我创建了这个函数以备不时之需。可以很容易地适应 LESS 的所有其他功能:
var lessColor = {
/*
|--------------------------------------------------------------------------
| Darken
|--------------------------------------------------------------------------
*/
darken: function(col, val){
col = col.replace(/#/g, ''); //Remove the hash
var color = new less.tree.Color(col); //Create a new color object
var amount = new less.tree.Value(val); //Create a new amount object
var newRGB = less.tree.functions.darken(color, amount); //Get the new color
var hex = (newRGB.rgb[0] + 256 * newRGB.rgb[1] + 65536 * newRGB.rgb[2]).toString(16);
hex = hex.split('.', 1); //Remove everything after the decimal if it exists
//Add padding to the hex to make it 6 characters
while(hex.length < 6){
hex = hex+'0';
}
hex = '#'+hex; //Add the hash
return hex; //return the color
}
}
你可以这样调用它:
$(this).css('background', lessColor.darken($(this).css('background'), .25);
最佳答案
编辑: darken 函数使用内置基元。
下面是变暗功能的使用方法
var color = new less.tree.Color([255, 255, 255], 0.5),
amount = new less.tree.Value(5);
less.tree.functions.darken(color, amount); // returns a Color object
关于javascript - 你如何调用 less.js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8199473/