【问题标题】:Accessing Javascript variable in Stylus in Meteor在 Meteor 的 Stylus 中访问 Javascript 变量
【发布时间】:2015-08-21 00:20:40
【问题描述】:

我正在使用 Meteor 制作游戏,我正在尝试使用 S-Grid 创建一个随机大小的网格,我在其中获取 Javascript 中的 columns 变量(实际上是咖啡脚本)并在 Stylus 中访问它们。

我的 .coffee 文件位于 lib 文件夹中,而我的 main.styl 文件位于客户端文件夹中。

random = (num) ->
  Math.floor Math.random() * num + 1

@cols = random()
@rows = random()

stylus = require('stylus')

stylus(str)
  .set('filename', '../client/main.styl')
  .define 'cols', stylus.nodes.unit(@cols)
  .define 'rows', stylus.nodes.unit(@rows)
  .render (err, css) ->
    if (err)
      throw err
    console.log(css)
    return

我在构建应用程序时收到此错误: node_modules/stylus/node_modules/glob/node_modules/minimatch/node_modules/lru-cache/test/memory-leak.js:1:15: Unexpected token ILLEGAL node_modules/stylus/node_modules/source-map/test/run-tests.js:1:15: Unexpected token ILLEGAL node_modules\stylus\node_modules\sax\examples\test.html:1: Can't set DOCTYPE here. (Meteor sets <!DOCTYPE html> for you) node_modules\stylus\node_modules\sax\LICENSE-W3C.html:1: Can't set DOCTYPE here. (Meteor sets <!DOCTYPE html> for you) The pure Stylus grid works fine but i can't seem to be able to set the columns through Javascript.


我这样定义我的网格

.grid
  grid()
   .box
     cells(1, @cols)

其中@cols 是来自 javascript 变量的随机数。如果我使用通用数字或使用 Stylus 生成随机数,它可以正常工作,但我需要 JS 变量(我将它用于其他事情)。我不知道如何在 .styl 中制作并在 JS 中访问它。

【问题讨论】:

    标签: javascript meteor coffeescript stylus


    【解决方案1】:

    在一个样式文件中定义这个函数并包含它:

    random(num)
        return math(math(0, 'random') * num + 1, 'floor')
    

    像往常一样在您的手写笔文件中使用随机。

    【讨论】:

    • 我试过了,它可以工作,但问题是我需要在 Javascript 和 CSS 中使用相同的随机数。到目前为止,我有两个随机数,一个在咖啡文件中,另一个在 styl 文件中。
    • 在javascript中,您可以使用jquery从元素中读取生成的数字
    • 我试过了,但是 S-Grid 没有生成属性或我可以访问的任何东西。不是我见过的,我一直在寻找 Firefox 开发者工具,但到目前为止没有.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2015-09-08
    • 1970-01-01
    • 2013-09-17
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多