首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以更新媒体查询中的SASS变量吗?

我可以更新媒体查询中的SASS变量吗?
EN

Stack Overflow用户
提问于 2016-11-22 10:41:17
回答 3查看 7.9K关注 0票数 8

考虑下面的SASS代码。我想确保如果屏幕高于1250px,那么margin-top应该是750px,然后它应该根据大小而改变。但是,SASS不允许我更新字符串中的变量。

代码语言:javascript
复制
// Above 1250px
$pageTemplateMargin:750px;

// Below 1250px
@media screen and (max-width:1250px){
    $pageTemplateMargin:550px;
}

// Below 950px
@media screen and (max-width:950px){
    $pageTemplateMargin:450px;
}

@media screen and (max-width:850px){
    $pageTemplateMargin:150px;
}

@media screen and (max-width:750px){
    $pageTemplateMargin:250px;
}

// Render the correct code
.page-template {margin-top:$pageTemplateMargin}

有更好的方法吗,因为它不工作,page-template停留在750px上。

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-22 12:31:02

我同意公认的答案,即在这种情况下最好使用maps,但我想指出几点。

变量实际上可以在媒体查询中更新。问题是,在块之外定义的变量是全局变量,而在块中定义的变量是局部变量变量。您可以让sass使用!全局关键字将块中的变量视为全局变量。

代码语言:javascript
复制
$pageTemplateMargin:750px;

@media screen and (max-width:1250px){
    $pageTemplateMargin: 550px !global;
}

.page-template {
  margin-top: $pageTemplateMargin //will use 550px instead of 750px
}

只想澄清这是可能的,尽管在这个用例中是不合适的。

我还建议您的代码使用loop,这将被证明是有帮助的,特别是当您添加更多的屏幕宽度和边距属性时,这样您就不需要进一步编写更多的媒体查询了。

代码语言:javascript
复制
$breakpoints: (
  1200px: 10px,
  1000px: 15px,
  800px: 20px,
);

@each $width, $margin in $breakpoints {
  @media screen and (max-width: $width) {
    .element {
      margin-top: $margin;
    }
  }
}

希望这能有所帮助。

票数 23
EN

Stack Overflow用户

发布于 2016-11-22 10:53:11

不,你不能(在这种情况下,正如在另一个答案中指出的那样)。

我建议使用mixins来处理这个问题:

代码语言:javascript
复制
@mixin pageTemplateMargin($px) {
    margin-top: $px
}

@media screen and (max-width:1250px) {
    .element { @include pageTemplateMargin(10px);}
}
@media screen and (max-width:1000px) {
    .element { @include pageTemplateMargin(15px);}
}
@media screen and (max-width:800px) {
    .element { @include pageTemplateMargin(20px);}
}

还有一种通过sass对象进行映射的方法,例如:

代码语言:javascript
复制
$breakpoints: (
  1200: 10px,
  1000: 15px,
  800: 20px,
);

@media screen and (max-width:1200px) {
    .element { margin-top: map-get($breakpoints, 1200);}
}
@media screen and (max-width:1000px) {
    .element { margin-top: map-get($breakpoints, 1000);}
}
@media screen and (max-width:800px) {
    .element { margin-top: map-get($breakpoints, 800);}
}

这将允许您通过调整1变量来全局更改边距。

工作代码示例

票数 6
EN

Stack Overflow用户

发布于 2019-01-14 20:45:01

我试过了然后我解决了我的问题。它将根据给定的速率(基大小/速率大小)自动计算所有媒体断点。

代码语言:javascript
复制
$base-size: 16;
$rate-size-xl: 24;

    // set default size for all cases;
    :root {
      --size: #{$base-size};
    }

    // if it's smaller then LG it will set size rate to 16/16;
    // example: if size set to 14px, it will be 14px * 16 / 16 = 14px
    @include media-breakpoint-down(lg) {
      :root {
        --size: #{$base-size};
      }
    }

    // if it is bigger then XL it will set size rate to 24/16;
    // example: if size set to 14px, it will be 14px * 24 / 16 = 21px
    @include media-breakpoint-up(xl) {
      :root {
        --size: #{$rate-size-xl};
      }
    }

@function size($px) {
   @return calc(#{$px} / $base-size * var(--size));
}

div {
  font-size: size(14px);
  width: size(150px);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40739695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档