简体   繁体   English

如何将参数从javascript传递到less

[英]How to pass parameter from javascript to less

Suppose to have this javascript code:假设有这个 javascript 代码:

<div component="user/picture" data-uid="' + post_show.uid + '" class="user-icon icona('+ post_show.icon_bgColor+')" data-original-title="" title="">' + post_show.icon_text + '</div></a></div>

I want pass icona less function the variable icon_bgColor .我想通过icona less 函数变量icon_bgColor This is my less code:这是我的较少代码:

.icona ( @iconcina ){

     background-color: @iconcina;
     margin-right: 15px;
     border-radius: 50%;
     width: 46px;
     height: 46px;
     line-height: 46px;
     font-size: 2.4rem;
}

Is it possibile pass a variable from javascript in less?是否有可能以更少的方式从 javascript 传递变量? Anyone can help me?任何人都可以帮助我吗?

Typically, LESS runs at build time.通常,LESS 在构建时运行。 It generates static files which are then deployed to the server.它生成静态文件,然后将其部署到服务器。

JavaScript runs at runtime, it takes the HTTP responses from the server and acts on them. JavaScript 在运行时运行,它从服务器获取 HTTP 响应并对其进行操作。

This means that they run in a strict order: LESS then JS.这意味着它们以严格的顺序运行:LESS 然后 JS。

There is no way to pass data in the other direction.没有办法在另一个方向传递数据。


An alternative approach would be to generate different rulesets with different selectors, which each of your values:另一种方法是使用不同的选择器生成不同的规则集,其中每个值:

.icona.foo {
    background-color: black;
}

.icona.bar {
    background-color: white;
}

… and then assign those class names to elements in JavaScript. ...然后将这些类名分配给 JavaScript 中的元素。

LESS gets compiled into static CSS, so there's no way to access variables or functions at runtime. LESS 被编译成静态 CSS,因此无法在运行时访问变量或函数。

You can either use javascript to assign new values to specific CSS properties with element.style.(property) , or use CSS native variables which are pretty new thing that allows to be modified at runtime.您可以使用 javascript 使用element.style.(property)为特定的 CSS 属性分配新值,或者使用 CSS 原生变量,这些变量是允许在运行时修改的非常新的东西。

You need to declare them at the :root element in CSS, then change them from javascript with document.documentelement.style.setproperty您需要在 CSS 中的 :root 元素中声明它们,然后使用document.documentelement.style.setproperty从 javascript 更改它们

Bear in mind they lack support for IE and some mobile browsers.请记住,它们不支持 IE 和某些移动浏览器。

check this source for more info检查此来源以获取更多信息

It seems that as said @Quentin there is no way to pass a js variable to .less file in the run time.似乎正如@Quentin所说,在运行时无法将 js 变量传递给.less文件。 Yet another approach to bypass this and to achieve a result is to use conditions, require() and multiple .less files.绕过这个并实现结果的另一种方法是使用条件、 require()和多个.less文件。 This way you emulate variables:这样你就可以模拟变量:

myLight.style.less

body {
  color: black;
  background-color: white;
}

myDark.style.less

body {
  color: white;
  background-color: black;
}

and then in our javascript code:然后在我们的 javascript 代码中:

myGreat.js

if(myVar === true) {
  require('./myLight.style.less')
}
else {
  require('./myDark.style.less')
}

Have you tried adding an attribute or class property using jquery eg This way you can dynamically set the properties of the class.您是否尝试过使用 jquery 添加属性或类属性,例如这样您可以动态设置类的属性。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

var textH = $(".Text").height();
var vertAlign = ((140 - textH)/2);
var textColor = 'red';
var backColor = 'black';
$(".Text").css({
    'margin-top': vertAlign,
     'color': textColor,
     'background-color': backColor
});

});
</script>
<script>

</script>

<div class="Text ">

hello world

</div>

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

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