Stylus CSS 媒体查询中的变量

28
我尝试了所有方法,但无法让Stylus CSS预处理器在媒体查询中使用变量。
例如,以下代码无法正常工作:
t = 1000px

@media screen and (max-width: t)
    html
        background blue

有人知道如何做这个吗?


1
这可能有帮助,但是Sass可以轻松实现 - bookcasey
我知道,我相信LESS也可以做到,但我真的更喜欢Stylus的语法,这是我无法跨越的唯一障碍。 :( - Cory
10个回答

32

看起来Stylus提供了一种更加清晰的方法来完成与此拉取请求相同的事情。

在这里,给定一个块大小,我可以创建样式来将容器居中于页面,并根据浏览器大小将容器大小设置为1、2或3个块宽。使用媒体查询作为变量(而不是将变量放置在媒体查询行内)使其比使用上面提到的unquote方法更加简洁。

/* in app.styl */

full_block_width = 300px

three_blocks = "all and (min-width: " + 3*full_block_width + ")"
two_blocks = "all and (min-width: " +  2*full_block_width + ") and (max-width: " + (3*full_block_width - 1) + ")"
one_block = "all and (max-width: " + (2*full_block_width - 1) + ")"

.container 
  margin: auto

@media three_blocks
  .container 
    width: (3*full_block_width)

@media two_blocks
  .container 
    width: (2*full_block_width)

@media one_block
  .container 
    width: full_block_width

25

很遗憾,你不能在媒体查询中使用变量或插值。我昨天遇到了类似的任务,并想出了以下解决方案:

t = 1000px

unquote("@media screen and (max-width: " + t + ") {")
html
    background blue
unquote("}")

这并不美观,但它有效 - 你可以使用unquote 来解决大部分 Stylus 相关问题。

6

在Stylus的0.43.0版本中,媒体查询可以像你在例子中展示的那样使用冒号,也可以不使用冒号:

t = 1000px

@media screen and (max-width t)
    html
        background blue

via Github


5
这是我亲身经历的有效方法。
medium = 'screen and (min-width: 768px)'
large = 'screen and (min-width: 992px)'
xlarge = 'screen and (min-width: 1200px)'

.box
    background: #000
    @media medium
        background: #111
    @media large
        background: #222
    @media xlarge
        background: #333

4

现在这已经被支持,默认情况下,官方页面摘录:

foo = 'width'
bar = 30em
@media (max-{foo}: bar)
body
    color #fff

3
现在应该可以工作了:
t = 1000px

@media screen and (max-width: t)
    html
        background blue

http://stylus-lang.com/docs/media.html

从文档中可以了解到:

插值和变量

您可以在媒体查询内部使用插值和变量,这样可以做到以下这些事情:

foo = 'width'
bar = 30em
@media (max-{foo}: bar)
  body
    color #fff

这将产生以下结果。
@media (max-width: 30em) {
  body {
    color: #fff;
  }
}

在MQ中也可以使用表达式:

.foo
  for i in 1..4
    @media (min-width: 2**(i+7)px)
      width: 100px*i

会让步给
@media (min-width: 256px) {
  .foo {
    width: 100px;
  }
}
@media (min-width: 512px) {
  .foo {
    width: 200px;
  }
}
@media (min-width: 1024px) {
  .foo {
    width: 300px;
  }
}
@media (min-width: 2048px) {
  .foo {
    width: 400px;


 }
}

这还不是全部。例如,我无法执行以下操作:large-max = '(max-width: ' + upper-bound(large-range) + ')' @media screen {small-min} and {large-max}``` - Kevin Ghadyani

3
我写了一个mixin,虽然不是完全理想的方案:
// media
media(args...)
  output = null
  for arg in args
    // check for tuple
    if arg[1]
      push(output,unquote('(%s: %s)' % (arg[0] arg[1])))
    else
      push(output,unquote(arg))

  unquote(s('%s',output))

可以这样使用:
$_media = media(screen,'and',(min-width $screen-tablet))
@media $_media
  .container
    max-width 728px

CSS 输出:
@media  screen and (min-width: 768px) {
  .container {
    max-width: 728px;
  }
}

1
如果我可以提供一种干净易读的方法,我会像这样利用哈希表的优势:
// Custom media query sizes
--query = {
    small: "screen and (min-width: 554px)",
    medium: "screen and (min-width: 768px)",
    large: "screen and (min-width: 992px)",
    extra-large: "screen and (min-width: 1200px)",
}

而且我会举个例子来调用它:

.main-logo
    font-large(--font.uni-sans-heavy)
    position: relative
    top: 50px
    left: 35px

    .logo-first
        color: --color.white
        margin-right: 3px

    .logo-second
        color: --color.bright-red

    @media --query.large
        left: 70px

非常明显,易于阅读。保持简单易懂。


0

我喜欢Tony Tai Nguyen的答案。这是另一种迭代:

sizes = {
  mobile: 0 768
  tablet: 769 1023
  desktop: 1024 1215
  widescreen: 1216 1407
  fullhd: 1408 99999999
}
query = {}

for name, pxs in sizes
  min = '(min-width:' + pxs[0] + 'px)'
  max = '(max-width:' + pxs[1] + 'px)'
  query[name] = min + ' and ' + max
  query[name + '-up'] = 'screen and ' + min
  query[name + '-down'] = 'screen and ' + max

// Usage: @media query.mobile or @media query.tablet-up etc...

0

我喜欢创建一个media混合器,使得无需为媒体查询创建命名变量:

media(query)
  @media query
    {block}

使用方法如下:

+media("screen and (min-width:" + width + "), print")
  .class
    foo: bar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接