我甚至不確定如何問這個問題。 LESS CSS框架包含幾個操作顏色的函數,我想知道如何自己調用這些函數來修改顏色。問題是,這些功能都位於另一個函數內定義的那樣:你怎麼稱呼less.js功能
(function (tree) {
tree.functions = {
darken: function(color, amount){
...stuff...
}
}
}
我非常清楚,假設變暗是tree.functions的方法,但對我的生活不要」知道如何調用它,因爲它是匿名函數(函數(樹)。
[編輯] 從@pradeek得到解決後,我創造了這個功能櫃面有人需要它的裏面。可以輕鬆適應所有其他的功能少了:
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);
我有同樣的問題,我想你的解決方案,但它有不同的輸出,你可以在這裏回答我的問題? http://stackoverflow.com/questions/22620519/call-less-js-function-in-javascript-has-different-output-in-compare-node – minttux