【问题标题】:Using Variables in PostCSS在 PostCSS 中使用变量
【发布时间】:2016-04-08 03:44:46
【问题描述】:

我在为 PostCSS 创建插件时遇到问题。

要了解我想要做什么,请查看以下代码:

button {
     button: button;
     button-border: 3px solid #abcde1;
}

这是我想做的。

如果没有设置按钮边框,那么我希望有一个默认值:

border: none;

但是,如果设置了button-border,那么我想使用button-border中设置的值。在上面的例子中,这将是:

border: 3px solid #abcde1; 

我想我会通过设置一个变量来做到这一点,但我认为我在范围方面遇到了问题。这是我尝试过的代码:

       css.walkDecls(decl => {                  
             var buttonBorder = 'none',
            button = [
                         'cursor: pointer;',
                         'display: inline-block;',
                         'min-height: 1em;',
                         'outline: none;',
                         'border:' + buttonBorder
                    ],
          joinButton = button.join('');
        if (decl.prop === 'button-border') {
                         var buttonBorder = decl.value;
                          decl.remove();
               }

         if (decl.prop === 'button') {
                         decl.replaceWith(joinButton);
        }  
    });

知道我做错了什么以及如何做对吗?

谢谢,

莫舍

【问题讨论】:

    标签: javascript variables scope postcss


    【解决方案1】:

    这是我在 Postcss github 页面上收到的答案:https://github.com/postcss/postcss/issues/691#issuecomment-168686832

    我试过了,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2017-03-09
      • 2020-07-16
      • 2017-07-01
      • 2018-04-25
      • 2017-03-20
      • 2021-01-12
      相关资源
      最近更新 更多