css - 如何使用 sass/compass 缩短这个占位符转换 CSS 选择器?

标签 css css-selectors sass compass-sass

所以我决定使用 SASS 制作一个带有 CSS 过渡的漂亮的动画搜索框 ( demo )。我还想为占位符文本添加动画,其中涉及 four different pesudo-classes ,源代码现在如下所示:

input {
  background-image:url(search.png);
  background-repeat:no-repeat;
  background-size:20px 20px;
  background-position-x:12px;
  background-position-y:10px;
  background-color:#6E597E;
  box-shadow:0 0 5px #333 inset;
  border-radius:15px;
  border:0;
  padding:8px 10px 10px 45px;
  font-size:20px;
  font-weight:300;
  font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
  color:#BBB;
  width:100%;
  transition:all 0.5s ease;
  &:focus {
    outline:none;
    box-shadow:0 0 10px #333 inset;
    background-color:#85699A;
    color:#EEE;

    &::-webkit-input-placeholder { color:#DDD; }
    &:-moz-placeholder { color:#DDD; }
    &::-moz-placeholder { color:#DDD; }
    &:-ms-input-placeholder { color:#DDD; }
  }

  &::-webkit-input-placeholder {
     color: #BBB;
     transition:color 0.5s ease;
  }

  &:-moz-placeholder { /* Firefox 18- */
     color: #BBB;  
     transition:color 0.5s ease;
  }

  &::-moz-placeholder {  /* Firefox 19+ */
     color: #BBB;  
     transition:color 0.5s ease;
  }

  &:-ms-input-placeholder {  
     color: #BBB;  
     transition:color 0.5s ease;
  }
}

有什么方法可以使用 SASS 或 compass 来缩短/整理它吗?

最佳答案

您能做的最好的事情就是将占位符抽象为混合:

@mixin placeholder {
  &::-webkit-input-placeholder {
     @content;
  }

  &:-moz-placeholder { /* Firefox 18- */
     @content;
  }

  &::-moz-placeholder {  /* Firefox 19+ */
     @content;
  }

  &:-ms-input-placeholder {  
     @content;
  }
}

input {
  background-image:url(search.png);
  background-repeat:no-repeat;
  background-size:20px 20px;
  background-position-x:12px;
  background-position-y:10px;
  background-color:#6E597E;
  box-shadow:0 0 5px #333 inset;
  border-radius:15px;
  border:0;
  padding:8px 10px 10px 45px;
  font-size:20px;
  font-weight:300;
  font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
  color:#BBB;
  width:100%;
  transition:all 0.5s ease;
  &:focus {
    outline:none;
    box-shadow:0 0 10px #333 inset;
    background-color:#85699A;
    color:#EEE;

    @include placeholder { color:#DDD; }
  }

  @include placeholder {  
     color: #BBB;  
     transition:color 0.5s ease;
  }
}

关于css - 如何使用 sass/compass 缩短这个占位符转换 CSS 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748139/

相关文章:

javascript - 链接到 Javascript 来源的内容

html - 调整窗口大小时保持元素与背景的相对位置?

css - Mega Drop Down 周围的边框

python - 使用 selenium python 使用复合类解析 HTML 内容

css - Sass map - 响应式排版

html - 如何向此时间轴添加其他部分?

javascript - 通知 JS 在固定元素上无法正常工作?

css - 如何在CSS中设置父子之间的边界?

java - 在selenium中选择cssselection时显示异常

sass - 如何将 stylus 文件转换为 scss 文件以更方便?