本文介绍了您如何调用less.js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不知道该怎么问. LESS CSS框架包含一些用于操纵颜色的函数,我想知道如何自己调用这些函数来修改颜色.问题在于这些函数位于另一个函数内部并定义为:

I'm not even really sure how to ask this. The LESS CSS Framework contains several functions to manipulate color, I'd like to know how to call these functions myself to modify a color. The problem is that these functions are located inside another function and defined as such:

(function (tree) {
tree.functions = {
    darken: function(color, amount){
        ...stuff...
    }
}
}

我足够知道 darken tree.functions 的一种方法,但是我一生都不知道如何调用它,因为它在内部匿名函数(函数(树)).

I know enough to assume that darken is a method of tree.functions, but for the life of me don't know how to call it because it is inside of the anonymous function (function (tree).

从@pradeek获得解决方案后,我创建了此函数,以防万一有人需要它.可以轻松适应LESS的所有其他功能:

[edit]After getting a solution from @pradeek I created this function incase anyone needs it. Can easily be adapted to all the other functions LESS has:

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函数使用内置基元.

The darken function uses built-in primitives.

这里是使用变暗功能的方法

Here's how to use the darken function

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

这篇关于您如何调用less.js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:21