我尝试了所有方法,但无法让Stylus CSS预处理器在媒体查询中使用变量。
例如,以下代码无法正常工作:
例如,以下代码无法正常工作:
t = 1000px
@media screen and (max-width: t)
html
background blue
有人知道如何做这个吗?
看起来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
很遗憾,你不能在媒体查询中使用变量或插值。我昨天遇到了类似的任务,并想出了以下解决方案:
t = 1000px
unquote("@media screen and (max-width: " + t + ") {")
html
background blue
unquote("}")
unquote
来解决大部分 Stylus 相关问题。在Stylus的0.43.0版本中,媒体查询可以像你在例子中展示的那样使用冒号,也可以不使用冒号:
t = 1000px
@media screen and (max-width t)
html
background blue
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
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// 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
@media screen and (min-width: 768px) {
.container {
max-width: 728px;
}
}
// 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
非常明显,易于阅读。保持简单易懂。
我喜欢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...
我喜欢创建一个media
混合器,使得无需为媒体查询创建命名变量:
media(query)
@media query
{block}
使用方法如下:
+media("screen and (min-width:" + width + "), print")
.class
foo: bar