如何调用 less.js 函数
我什至不太确定如何问这个问题。 LESS CSS框架包含几个操作颜色的函数,我想知道如何自己调用这些函数来修改颜色。问题是这些函数位于另一个函数内部并定义如下:
(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);
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...
}
}
}
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).
[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
}
}
And you can call it like so:
$(this).css('background', lessColor.darken($(this).css('background'), .25);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
darken 函数使用内置原语。
下面介绍如何使用变暗功能
EDIT:
The darken function uses built-in primitives.
Here's how to use the darken function
看LESS 1.7未缩小的代码 对在这里。
第 141 行是这样的:
并且位于全局范围内。所以
less
对象是在浏览器中定义的。接下来,查看
1254
行:您的
darken
函数是在其中的某处定义的。您可以像这样调用
darken
:Look at the un-minified code of LESS 1.7 right here.
Line
141
is this:And is in the global scope. So the
less
object is defined in the browser.Next, look at line
1254
:Your
darken
function is defined somewhere in there.You can call
darken
like so:虽然这个答案不会告诉您如何调用 less.js 函数来操纵颜色,但它确实提供了一种不同的方法来操纵颜色,因为这似乎是主要目标。
有一个方便的 JavaScript 库专门用于此目的:TinyColor。
要使颜色变暗,只需
While this answer won’t tell you how to call less.js functions to manipulate color, it does provide a different approach to manipulate color, as this seems to be the main goal.
There exists a handy JavaScript library just for that: TinyColor.
To darken a color, simply