从 JavaScript 检索或设置 LESS 变量?

发布于 2024-12-18 09:46:28 字数 249 浏览 4 评论 0原文

在测试我的网络应用程序时,我希望能够使用简单的下拉菜单设置我的 LESS @variables 之一的值。 (完全改变配色方案)。

我想设置该值后,LESS 必须使用新值重新加载/重新编译 .less 文件?

有什么简单的方法可以实现这一点吗? (我没有运行node.js)

基本示例如下:

styles.less
@base-color = `window.baseColor()`

While testing my web app, I'd like to be able to set the value of one of my LESS @variables using a simple dropdown. (To change the color scheme altogether).

I guess after setting the value, LESS has to reload/recompile the .less files with the new value?

Is there any simple way of accomplishing this? (I'm not running node.js)

The basic example would be something like:

styles.less
@base-color = `window.baseColor()`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-12-25 09:46:28

我认为你这样做的方式有点太复杂了;)

这是我的建议:

  1. 在你的 body 标签上设置一个类:

    ;
    
  2. 在你的 .less 文件中定义你的主题:

    body.theme_default { base_color: #fff; }
    body.theme_gray { base_color: #ccc; }
    ETC..
    
  3. 使用 jQuery(或者只是普通的 JS)在下拉状态改变时改变 body 标签的类。

这就是我要做的(并用一些漂亮的小部件替换下拉列表;)
干杯

I think you're doing it in a bit too complicated way ;)

Here's what I propose:

  1. Set a class on your body tag:

    <body class='theme_default'></body>
    
  2. In your .less file define your themes:

    body.theme_default { base_color: #fff; }
    body.theme_gray    { base_color: #ccc; }
    etc..
    
  3. Using jQuery (or just plain JS) change the class of the body tag upon dropdown state change.

That's how I'd do it (and replace the dropdown with some nice widget ;)
Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文