动态加载 less.js 规则

发布于 2024-09-07 19:16:03 字数 2220 浏览 7 评论 0 原文

我正在考虑使用 lessjs (看起来很棒),但我们的网站要求在初始页面加载后动态加载某些样式。然而,似乎所有 LESS 样式表都必须在 less.js 脚本加载之前加载。即,这可以工作

<link rel="stylesheet/less" href="/static/less/style.less"/>
<script src="http://lesscss.googlecode.com/files/less-1.0.30.min.js"></script>

,但如果交换行,则会失败,firefox 和 chrome 似乎都不会尝试加载“style.less”,除非它们的顺序正确。 本教程中明确注明了订购要求。

有没有办法在初始页面加载后加载更少的样式表?

请注意这个博客描述了“观看”功能 -

每当您保存 LESS 代码时,它都会自动刷新 CSS

,因此我可以合理地期望我可以在页面加载后添加一些 LESS 规则。感觉好像我错过了一些东西。

干杯,

科林

更新:用于测试注释中描述的行为的代码(脚本后列出的较少样式表) -

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Simple</title>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script src="/static/js/less-1.0.31.min.js"></script> 
  <link rel="stylesheet/less" href="/static/less/style.less" id="abc123"/>
</head>
<body>
  <div id="container">
    <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div>
  </div>
  <div id="#abc">Bingo</div>
</body>

<script>
console.log("refreshing styles...");
less.sheets.push(document.getElementById('abc123'));
//var lessStyle = $("<style>#abc { color: blue; }</style>").attr("id", "less:static-less-style").attr("type", 'text/less');
//$("head").append(lessStyle);
less.refresh(true);
console.log("refreshed...");
</script>
</html>

以及较少的样式表

@primary_color: green;

.rounded(@radius: 5px) {  
  -moz-border-radius: @radius;  
  -webkit-border-radius: @radius;  
  border-radius: @radius;  
}

#container {
  background: @primary_color;
  .rounded(5px);

  div {
    color: red;
  }
}

I'm looking at using less.js (looks great), but our site requires that some styles be loaded dynamically after initial page load. It seems, however, that all LESS stylesheets must be loaded prior to the less.js script load. i.e. this works

<link rel="stylesheet/less" href="/static/less/style.less"/>
<script src="http://lesscss.googlecode.com/files/less-1.0.30.min.js"></script>

but it fails if the lines are swapped around, neither firefox nor chrome appear to attempt loading 'style.less' unless they are ordered correctly. The ordering requirement is noted explicitly in this tutorial.

Is there any way to load less stylesheets after initial page load?

Note that this blog describes a 'watch' feature -

which will auto-refresh the CSS whenever you save your LESS code

so it seems reasonable to expect that I could add some LESS rules after page load. Feels like I'm missing something.

Cheers,

Colin

UPDATE: code used to test behaviour described in comments (less style sheet listed after the script) -

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Simple</title>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script src="/static/js/less-1.0.31.min.js"></script> 
  <link rel="stylesheet/less" href="/static/less/style.less" id="abc123"/>
</head>
<body>
  <div id="container">
    <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div>
  </div>
  <div id="#abc">Bingo</div>
</body>

<script>
console.log("refreshing styles...");
less.sheets.push(document.getElementById('abc123'));
//var lessStyle = $("<style>#abc { color: blue; }</style>").attr("id", "less:static-less-style").attr("type", 'text/less');
//$("head").append(lessStyle);
less.refresh(true);
console.log("refreshed...");
</script>
</html>

and the less stylesheet

@primary_color: green;

.rounded(@radius: 5px) {  
  -moz-border-radius: @radius;  
  -webkit-border-radius: @radius;  
  border-radius: @radius;  
}

#container {
  background: @primary_color;
  .rounded(5px);

  div {
    color: red;
  }
}

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

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

发布评论

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

评论(4

寄居人 2024-09-14 19:16:04

感谢您提出这个问题——这对我帮助很大。我只是更新主题,以展示我如何动态添加 CSS 文件。

我使用了 LESS 的最新版本(1.3.3)。

var stylesheetFile = 'file.css';
var link  = document.createElement('link');
link.rel  = "stylesheet";
link.type = "text/less";
link.href = stylesheetFile;
less.sheets.push(link);

less.refresh();

thanks for asking this question – it helped me a lot. I’m just updating the topic in order to show how I did to dynamically add a CSS file.

I used the last version of LESS (1.3.3).

var stylesheetFile = 'file.css';
var link  = document.createElement('link');
link.rel  = "stylesheet";
link.type = "text/less";
link.href = stylesheetFile;
less.sheets.push(link);

less.refresh();
深海不蓝 2024-09-14 19:16:04

您可以使用这个轻量级 (3k) 库延迟加载 less / css 和 js 文件(免责声明:i我是作者)。

这很简单:

lazy.load('/static/less/style.less');

它还可以接收回调,加载更多具有依赖项的资源并处理缓存。

You can use this lightweight (3k) library to lazy load less / css and js files ( disclaimer: i am the author).

This is as simple as:

lazy.load('/static/less/style.less');

It can also receive a callback, load some more assets with dependencies and takes care about cache.

冷了相思 2024-09-14 19:16:04

我发现该脚本的最新版本可以与 LESS 文件完美配合:

  <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.0.0/less.min.js"></script>

而不是使用

less.refreshStyles()

use

less.refresh()

I found an up-to-date version of the script that works perfect with LESS file:

  <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.0.0/less.min.js"></script>

Instead using

less.refreshStyles()

use

less.refresh()
心安伴我暖 2024-09-14 19:16:03

我刚刚推送了 1.0.31 - 它有一个方法: less.refreshStyles() 它将使用 type="text/ 重新编译

I just pushed 1.0.31 — it has a method: less.refreshStyles() which will re-compile <style> tags with type="text/less" — try it out and let me know if it works.

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