【问题标题】:SASS Media Query MixinSASS Media Query Mixin
【发布时间】:2017-03-14 23:45:39
【问题描述】:

我正在尝试按照this 教程设置断点。当我只针对一个属性时,它们工作正常。当我尝试创建一个针对两个(宽度和高度)的查询时,它只显示后者。我研究了我正在使用的 Sass 函数,它应该可以正确渲染,所以我不确定问题出在哪里。我在 Ruby on Rails 版本 4.2.2 上使用 sass-rails。

感谢

$breakpoints: (
  'small'  : ( max-width:  20em ),
  'mobile' : ( min-width:  36.25em ),
  'tablet'  : ( min-width: 48em ),
  'desktop'  : ( min-width: 60em ),
  'wide'    : ( min-width: 75em ),
  'height'    : ( min-width: 75em ) and (min-height: 62.5em)  
);

@mixin media($name) {
  @if map-has-key($breakpoints, $name) {
    @media #{inspect(map-get($breakpoints, $name))} {
      @content;
    }
  }

  @else {
    @warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
        + "Please make sure it is defined in `$breakpoints` map.";
  }
}

这是渲染的:

@media (min-height: 62.5em) {

}

【问题讨论】:

    标签: css ruby-on-rails sass media-queries


    【解决方案1】:

    通常将媒体查询存储在变量中或作为键值对中的时,它们应该存储为字符串。查看这些网站 Media Queries by David WalshResponsive Web Design in Sass by Mason Wendell

    替换这行代码
    'height' : ( min-width: 75em ) and (min-height: 62.5em)

    'height' : "( min-width: 75em ) and (min-height: 62.5em)"

    既然传递给 mixin 的值可以是 stringlist,mixin 需要识别这些变化

    @mixin media($name) {
      @if map-has-key($breakpoints, $name) {
        $value: map-get($breakpoints, $name);
        $query: if(type-of($value) == "string", $value, inspect($value));
        @media #{$query} {
          @content;
        }
      }
      @else {
        @warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
            + "Please make sure it is defined in `$breakpoints` map.";
      }
    }
    
    $breakpoints: (
      'small'  : ( max-width:  20em ),
      'wide'    : "( min-width: 75em ) and (min-height: 62.5em) and (max-height: 100em)",
      'height'    : "( min-width: 75em ) and (min-height: 62.5em)"  
    );
    
    div.gta {
      @include media(wide) { ... }
    }
    
    div#mgs {
      @include media(height) { ... }
    }
    

    这将编译为以下 CSS

    @media (min-width: 75em) and (min-height: 62.5em) and (max-height: 100em) {
      div.gta { ... }
    }
    
    @media (min-width: 75em) and (min-height: 62.5em) {
      div#mgs { ... }
    }
    

    这将允许在任意数量的媒体查询中使用 mixin

    【讨论】:

    • 谢谢!这行得通,我不明白为什么它不像教程所说的那样工作,因为我从那里复制了它。你能解释一下if 声明中发生了什么吗?我从来没有见过这样的。我阅读了您建议的链接,希望能找到更多相关信息,但我找不到任何东西。
    • 链接真的表明复合媒体查询总是作为字符串存储在变量中。我会尝试看看教程。我在代码中使用了if function。它需要 3 个 args 逗号分隔,第一个是条件,第二个是返回值,如果条件计算为真,第三个是返回值,如果条件计算为假。一旦您对编写自己的媒体查询充满信心,我强烈建议您查看breakpoint。之后你将无法生活。
    • 感谢您的解释。所以它基本上是一个三元运算符。我没见过这样的。我认为这是一个SASS的事情。是的,我以前看过断点。我只是想了解更多关于 mixins 和函数的知识,这样我以后就可以自己编写了。这就是为什么我决定走这条路线。但我同意断点很棒。非常感谢您解释它和指针。这真的帮助了我。
    【解决方案2】:

    这是我想要定位多个媒体查询时使用的(显然将您的查询设置为您想要的任何内容):

    $size__site_content_width : 1024px;
    
    /* Media Queries */
    $media_queries : (
        'mobile'    : "only screen and (max-width: 667px)",
        'tablet'    : "only screen and (min-width: 668px) and (max-width: $size__site_content_width)",
        'desktop'   : "only screen and (min-width: ($size__site_content_width + 1))",
        'retina2'   : "only screen and (-webkit-min-device-pixel-ratio: 2) and (min-resolution: 192dpi)",
        'retina3'   : "only screen and (-webkit-min-device-pixel-ratio: 3) and (min-resolution: 288dpi)",
        'landscape' : "screen and (orientation:landscape) ",    
        'portrait'  : "screen and (orientation:portrait) "
    );
    
    @mixin for_breakpoint($breakpoints) {
        $conditions : ();
        @each $breakpoint in $breakpoints {
          // If the key exists in the map
          @if map-has-key($media_queries, $breakpoint) {
            $conditions: append(
                $conditions,
                #{inspect(map-get($media_queries, $breakpoint))},
                comma
            );
          }
          @else {
            @warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
            + "Please make sure it is defined in `$breakpoints` map.";
          }
        }
    
        @media #{$conditions} {
            @content;
        }
    
    }
    

    我们在我的 scss 中是这样的:

    #masthead {
        background: white;
        border-bottom:1px solid #eee;
        height: 90px;
        padding: 0 20px;
        @include for_breakpoint(mobile desktop) {
            height:70px;
            position:fixed;
            width:100%;
            top:0;
        }
    }
    

    然后输出这个:

    #masthead { 
      background: white;
      border-bottom: 1px solid #eee;
      height: 90px;
      padding: 0 20px;
    }
    
    @media only screen and (max-width: 667px), only screen and (min-width: 1025px) { 
      #masthead {
        height: 70px;
        position: fixed;
        width: 100%;
        top: 0;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 2011-10-31
      • 2014-07-07
      • 2023-04-02
      • 2016-12-11
      • 2018-10-24
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多