【问题标题】:Compass sprite - avoid using extends when including a sprite指南针精灵 - 包含精灵时避免使用扩展
【发布时间】:2013-03-24 01:20:32
【问题描述】:

我正在使用 Compass 生成我的精灵,它运行良好,但我遇到了一个小烦恼。在另一个@include 中时,我无法使用@include 语句包含单个精灵,例如我常用的媒体查询混合。我的精灵 SCSS 如下所示:

.sp {
    background-repeat: no-repeat;
    overflow: hidden;
    line-height: 0;
    font-size: 0;
    text-indent: 100%;
    border: 0;
}

$sp-sprite-dimensions: true;
$sp-sprite-base-class: '.sp';
$sprite-layout: smart;
@import "sp/*.png";
@include all-sp-sprites;

在另一个位置,我正在尝试这样做:

.logo {
    a {
        @include break($break1) {
            @include sp-sprite(logo-small);
        }
    }
}

SCSS 可以使用嵌套的@include 语句,但它不允许在@include 语句中使用@extend 语句,而且显然精灵@include 在幕后生成@extend 语句,这是我不想要的。有人知道解决这个问题的方法吗?

编辑:

@lolmaus 引起了我的注意,真正的问题是我在媒体查询中嵌套了一个 @extend。我想这是不允许的,有什么办法吗?

【问题讨论】:

  • 你好像在做一些奇怪的事情。 sp-sprite 是你编码的 mixin 吗?请提供完整代码。
  • @lolmaus 不,这是一个由 compass 自动生成的 mixin。请参阅此处的“选择器控件”:http://compass-style.org/help/tutorials/spriting/
  • 问题是可以在另一个 mixin 中使用 foo-sprite mixin。 [这个[(gist.github.com/lolmaus/2101b2938c665a3d44de) 编译得很好。这就是为什么我认为你已经覆盖了mixin。请提供完整的代码和错误文本,以便我们将报告的错误行与代码匹配。使用gist.github.com一次共享多个大文件。
  • 哦,我明白了。问题不在于您的扩展位于 mixin 中。问题是它在媒体查询中!
  • 哦,我明白了,我想这就是问题所在,感谢您跟踪它。有什么办法吗?

标签: css sass compass-sass


【解决方案1】:

在媒体查询中使用 Compass sprite 是不可能的,至少按照文档中描述的方式是不可能的。

有几种解决方法:

【讨论】:

  • 你如何使用自定义mixin,我试过了,它被包含在内。但我可能做错了什么
  • 1) 将 mixin 放在一个部分中,例如。 G。 _get-sprite.scss。 2)在代码开头的某处导入部分。 3)像这样应用mixin:.element { @include get-sprite(...); }。确保正确提供所有参数。 4) 编译。如果结果不满意,请分享: a) 你的 SASS 代码; b) 生成的 CSS 代码; c) 解释您想要达到的目标以及与您的预期不同的结果。
  • 哦,来吧,学习一下,太短了! PS不要忘记接受答案。
【解决方案2】:

这是一个 SASS (SCSS) mixin,用于生成可用于媒体查询的精灵声明块

SCSS:

// http://compass-style.org/reference/compass/helpers/sprites/
@mixin get-sprite($map, $sprite, $repeat: no-repeat, $height: true, $width: true) {

  //http://compass-style.org/reference/compass/helpers/sprites/#sprite-file
  $sprite-image: sprite-file($map, $sprite);

  // http://compass-style.org/reference/compass/helpers/sprites/#sprite-url
  $sprite-map: sprite-url($map);

  // http://compass-style.org/reference/compass/helpers/sprites/#sprite-position
  $sprite-position: sprite-position($map, $sprite);

  // Returns background
  background: $sprite-map $sprite-position $repeat;

  // http://compass-style.org/reference/compass/helpers/image-dimensions/
  // Checks to see if the user wants height returned
  @if $height == true {
    // Gets the height of the sprite-image
    $sprite-height: image-height($sprite-image);
    // Returns the height
    height: $sprite-height; }

  // http://compass-style.org/reference/compass/helpers/image-dimensions/
  // Checks to see if the user wants height returned
  @if $width == true {
    // Gets the width of the sprite-image
    $sprite-width: image-width($sprite-image);
    // Returns the width
    width: $sprite-width; }
}

用法:

$icons: sprite-map("sprites/icons/*.png"); // define a sprite map 

// ... later

@media only screen and (max-width: 500px) {
    .video .overlay {
        @include get-sprite($icons, play-btn-large);
    }
}

Source: GitHubGist - brubrant / get-sprite.scss

【讨论】:

  • 这行得通的唯一原因是您将媒体查询带到了 mixin 之外。我正在想办法把它留在里面。
【解决方案3】:

下面的代码描述了如何做到这一点

要点:@extend Compass sprites in @media queries

/*
 * A simple way to extend Compass sprite classes within media queries.
 * Based on the knowledge gained here: http://www.sitepoint.com/cross-media-query-extend-sass/
 * I admit it's nowhere near as clever, but it does work :)
 */


/*
 * Set-up sprites for each media size
 */

// default
@import "icons-sm/*.png"
@include all-icons-sm-sprites

// corresponding sprites for larger devices
// notice that @import is within the @media query
// that's critical!

@media (min-width: $large)
  @import "icons-lg/*.png"
  @include all-icons-lg-sprites

/*
 * Now you can do something like this
 */

// an example mixin

@mixin social-links($size)
  $socials: facebook, twitter, youtube
  @each $social in $socials
    &.#{$social}
      @extend .icons-#{$size}-#{$social}

/*
 * Put to use
 */

// assuming you've got mark-up like this

<p class="social">
  <a href="#" class="facebook">facebook</a>
  <a href="#" class="twitter">twitter</a>
  <a href="#" class="youtube">youtube</a>
</p>

// you can do this                          
.social
  a
    @include social-links(sm)
    width: 25px
    height: 25px
    @media (min-width: $large)
      @include social-links(lg)
      width: 50px
      height: 50px

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多