【问题标题】:Style input placeholder with Sass for focused fields使用 Sass 为重点字段设置样式输入占位符
【发布时间】:2016-12-22 20:23:02
【问题描述】:

我正在尝试在 Sass 3.3.1 中为输入字段(针对不同的浏览器)设置占位符的样式,并希望在字段获得焦点时更改不透明度。我很难将伪类和伪元素与&符号结合起来。下面给出编译错误:

::-webkit-input-placeholder,
:-moz-placeholder,
::-moz-placeholder,
:-ms-input-placeholder{

    ... some default styling

    :focus#{&}{
        opacity: 0;
    }
}

这个可以吗?

编辑

这是我正在寻找的输出:

::-webkit-input-placeholder {
    opacity: 1;
}
:-moz-placeholder{
    opacity: 1;
}
::-moz-placeholder{
    opacity: 1;
}
:-ms-input-placeholder{
    opacity: 1;
}
:focus::-webkit-input-placeholder {
    opacity: 0;
}
:focus:-moz-placeholder{
    opacity: 0;
}
:focus::-moz-placeholder{
    opacity: 0;
}
:focus:-ms-input-placeholder{
    opacity: 0;
} 

【问题讨论】:

  • 另请注意,此选择器不起作用:stackoverflow.com/questions/16982449/…
  • @cimmanon 感谢您的帮助,我不知道第二个链接。但我仍在寻找一种简单的方法来为每个供应商生成 :focus-rules。
  • :focus 没有什么特别之处,就 Sass 而言,它与任何其他选择器相同。您的答案在第一个链接中。也相关:stackoverflow.com/questions/17181849/placeholder-mixin-scss-css
  • 我看不到第一个链接将如何引导我找到输出 :focus::-webkit-input-placeholder 的内容。我不能使用:focus&,因为& 之前必须有一个空格。您的最后一个链接似乎更符合我的要求。再次感谢您的帮助。

标签: sass


【解决方案1】:
// Cross-browsers opacity: @include opacity(0.5);
@mixin opacity($opacity) {
    opacity: $opacity;
    $opacity-ie: $opacity * 100;
    filter: alpha(opacity=$opacity-ie); //IE8
}

// Transitions for all: @include transition($transition);
$transition: all .3s ease;
@mixin transition($value) {
    -webkit-transition: $value;
    -moz-transition: $value;
    -ms-transition: $value;
    -o-transition: $value;
    transition: $value;
}

// Input placeholder animation: @include placeholder { color: #000 }
@mixin placeholder {
    &::-webkit-input-placeholder {
        @content;
    }
    &:-moz-placeholder {
        @content;
    }
    &::-moz-placeholder {
        @content;
    }
    &:-ms-input-placeholder {
        @content;
    }
}

// How to use:
input {
    text-overflow: ellipsis;
    color: mediumseagreen;
    @include placeholder {
        color: cornflowerblue;
        transition: $transition;
        @include opacity(1);
    }
    &:focus {
        @include placeholder {
            @include opacity(0);
            transition: $transition;
        }
    }
}

【讨论】:

    【解决方案2】:

    实际上是相反的:

    element:focus{
        &::-webkit-input-placeholder,
        &:-moz-placeholder,
        &::-moz-placeholder,
        &:-ms-input-placeholder{
            opacity: 0;
        }
    }
    

    编辑

    在我的测试中组合它们似乎有问题,但以下应该可行:

    ::-webkit-input-placeholder{
       color: red;
       &:focus{
            color: blue;
       }
    }
    

    不过,需要注意的是,这只有在它们被分开时才有效。您不能将多个伪选择器组合到一个定义中(例如 ::-webkit-input-placeholder, :-moz-input-placeholder{ /* this does not work in my testing */ })。

    更新 2

    这是我模拟的一个快速 SASS 函数,可以简化流程:

    @mixin input-placeholder($all:default){
        @if $all == default {
            $all : ("::-webkit-input-placeholder", ":-moz-placeholder","::-moz-placeholder",":-ms-input-placeholder");
        }
        @each $placeholder in $all {
            #{unquote($placeholder)}{
                @content;
            }
        }
    }
    

    您可以通过以下方式使用它:

    @include input-placeholder{
        color: red;
        &:focus {
           color: blue;
        }
    }
    

    这意味着您只需编写一次代码。它将在单独的行上输出所有这些,并对它们应用相同的规则。

    【讨论】:

    • 当字段没有聚焦时,我也有一些默认的占位符样式。 2个可以合并吗?抱歉,如果我不清楚,我更新了原始问题。
    • @WillemVanBockstal 我已经通过回答进行了修改,你可以做到这一点,但它不是很有趣。您需要将每个伪类分开,否则 Safari(在我的情况下)只是这样做;不应用它们。
    • @WillemVanBocstal 我已经在上面测试了我的 SASS 函数,它输出的代码在我的测试中可以在 safari 8m 中运行,并且应该也可以在所有其他浏览器上运行。
    • 我认为在您的第一次编辑中,顺序仍然是错误的,因为我需要以这个特定的顺序编译为 :focus::-webkit-input-placeholder 。但是谢谢,你在更新 2 中是对的,我需要分离每个伪类,所以我要试试这个功能!
    • @WillemVanBockstal 在这种情况下,元素的顺序并不重要,因为唯一的要求是两者都为真。因此,如果它的焦点和选中,则将颜色设为蓝色,否则将颜色设为红色。您实际上不需要为伪元素提供任何要应用的元素,因此第二个可以工作并输出代码,据我所知:)
    【解决方案3】:

    来自 SASS Compass 的解决方案:

    // Style the html5 input placeholder in browsers that support it.
    //
    // The styles for the input placeholder are passed as mixin content
    // and the selector comes from the mixin's context.
    //
    // For example:
    //
    //     #{elements-of-type(text-input)} {
    //       @include input-placeholder {
    //         color: #bfbfbf;
    //         font-style: italic;
    //       }
    //     }
    //
    // if you want to apply the placeholder styles to all elements supporting
    // the `input-placeholder` pseudo class (beware of performance impacts):
    //
    //     * {
    //       @include input-placeholder {
    //         color: #bfbfbf;
    //         font-style: italic;
    //       }
    //     }
    @mixin input-placeholder {
      @include with-each-prefix(css-placeholder, $input-placeholder-support-threshold) {
        @if $current-prefix == -webkit {
          &::-webkit-input-placeholder { @content; }
        }
        @elseif $current-prefix == -moz {
          // for Firefox 19 and below
          @if support-legacy-browser("firefox", "4", "19", $threshold: $input-placeholder-support-threshold) {
            &:-moz-placeholder { @content; }
          }
          // for Firefox 20 and above
          &::-moz-placeholder { @content; }
        }
        @elseif $current-prefix == -ms {
          &:-ms-input-placeholder { @content; }
        }
      }
      // This is not standardized yet so no official selector is generated.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-07
      • 2013-01-15
      • 2014-07-12
      • 2020-12-29
      • 2019-01-27
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多