css - SASS Mixin 未产生预期结果

标签 css sass mixins

我第一次在元素中使用 SASS,我喜欢它。我再也不想写纯 CSS 了。然而,我对以下场景有点困惑,希望我错过了一些非常明显的东西。

首先,我有以下 SCSS(包括 CSS 输出):

// SCSS
.pos-button {
    @include linear-gradient($color-secondary-green, !important);
    @include text-shadow(lighten($color-secondary-green, 10%), !important);
    &:active {
        box-shadow:inset 0 0 5px 2px darken($color-secondary-green, 20%) !important;
    }
    &:hover {
        @include linear-gradient(darken($color-secondary-green, 10%), !important);
        @include text-shadow($color-secondary-green);
    }
}

// CSS OUTPUT    
.pos-button {
  background: #99c965 !important;
  background: -moz-linear-gradient(#99c965, #669434) !important;
  background: -webkit-linear-gradient(#99c965, #669434) !important;
  background: -o-linear-gradient(#99c965, #669434) !important;
  background: -ms-linear-gradient(#99c965, #669434) !important;
  background: linear-gradient(#99c965, #669434) !important;
  text-shadow: 0 1px #99c965 !important; }
  .pos-button:active {
    box-shadow: inset 0 0 5px 2px #4c6e27 !important; }
  .pos-button:hover {
    background: #80ba41 !important;
    background: -moz-linear-gradient(#80ba41, #4c6e27) !important;
    background: -webkit-linear-gradient(#80ba41, #4c6e27) !important;
    background: -o-linear-gradient(#80ba41, #4c6e27) !important;
    background: -ms-linear-gradient(#80ba41, #4c6e27) !important;
    background: linear-gradient(#80ba41, #4c6e27) !important;
    text-shadow: 0 1px #80ba41; }

现在,问题来了。我想将上面的内容重构为 mixin,但是 :hover 属性没有出现在 CSS 输出中:

// SCSS
.pos-button {
    @include color-buttons($color-secondary-green);
}

@mixin color-buttons($color) {
    @include linear-gradient($color, !important);
    @include text-shadow(lighten($color, 10%), !important);
    &:active {
        box-shadow:inset 0 0 5px 2px darken($color, 20%) !important;
    }
    &:hover {
        @include linear-gradient(darken($color, 10%), !important);
        @include text-shadow($color);
    }
}


// CSS OUTPUT
.pos-button {
  background: #99c965 !important;
  background: -moz-linear-gradient(#99c965, #669434) !important;
  background: -webkit-linear-gradient(#99c965, #669434) !important;
  background: -o-linear-gradient(#99c965, #669434) !important;
  background: -ms-linear-gradient(#99c965, #669434) !important;
  background: linear-gradient(#99c965, #669434) !important;
  text-shadow: 0 1px #99c965 !important; }
  .pos-button:active {
    box-shadow: inset 0 0 5px 2px #4c6e27 !important; }

知道问题是什么,或者我是否遇到了 SASS 的限制?我可以将 :active:hover 属性拆分为它们自己的 mixins,但我真的很想将其合并为一个,因为我试图保留这个元素尽可能干燥。

谢谢!

编辑

混合:

@mixin text-shadow($color, $important : null) {
    text-shadow:0 1px $color $important;
}

@mixin linear-gradient($color, $important : null) {
    $from:lighten($color, 10%);
    $to:darken($color, 10%);
    background: $from $important; // Old browsers
    background: -moz-linear-gradient($from, $to) $important; // FF3.6+
    background: -webkit-linear-gradient($from, $to) $important; // Chrome10+,Safari5.1+
    background: -o-linear-gradient($from, $to) $important; // Opera 11.10+
    background: -ms-linear-gradient($from, $to) $important; // IE10+
    background: linear-gradient($from, $to) $important; // W3C
}

最佳答案

对我来说效果很好:http://sassmeister.com/gist/5458986所以肯定是你的代码结构有问题。

我注意到您在声明之前调用了 mixin。这应该会引发错误,除非您之前定义了 mixin 并且您试图重新定义它。您最初定义它时是否没有 &:hover 部分?

请仔细检查您没有搬起石头砸自己的脚。

关于css - SASS Mixin 未产生预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209575/

相关文章:

javascript - 添加内容时设置空div的高度保持静态

javascript - 将 D3 线条样式改回常规

css - 使用 Angular 5 和 Flex 布局的 Div 之间的距离相等

jquery - 可变高度水平 Accordion

node.js - 不导出文件时,Webpack 无法正确渲染 sass

javascript - 重构遗留的基于 mixin 的类层次结构

css - 在另一个 mixin 中使用来自 LESS 的一个 mixin

html - Bootstrap 网站 jumbotron 图像缩放

variables - Less > 在常量上定义变量

css - 有条件地覆盖 scss 变量以进行 bootstrap 4 主题切换