【问题标题】:Extend &:hover of any class to any other class's hover in SASS or SCSS将任何类的 &:hover 扩展到任何其他类在 SASS 或 SCSS 中的悬停
【发布时间】:2016-03-24 20:40:24
【问题描述】:

有没有办法将任何类的&:hover 扩展到任何其他类的悬停?

我的代码是这样的,它停止了两个类的悬停:

HTML

<a href="#" class="button1">Button 1</a>
<a href="#" class="button2">Button 2</a>

SCSS

.button1 {
  background: black;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    background: red;
  }
}

.button2 {
  background: blue;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
  &:hover {
    @extend .button1:hover
  }
}

【问题讨论】:

标签: html css sass hover


【解决方案1】:

您可以使用mixin

@mixin hoverStyle {
   &:hover{
     background: red;
   }
}

.button1 {
   background: blue;
   padding: 5px 10px;
   text-decoration: none;
   color: white;
   @include hoverStyle;
}

.button2 {
   background: blue;
   padding: 5px 10px;
   text-decoration: none;
   color: white;
   @include hoverStyle;
}

【讨论】:

    【解决方案2】:

    试试这个解决方案。它对我有用 http://jsbin.com/lezuwalida/edit?html,css,output

    生成的 css 看起来一样

    .demon:hover, .demon.demo {
      outline: 3px solid red;
    }
    
    .demon2:hover, .demon2.demo2 {
      outline: 3px solid red;
    }
    

    【讨论】:

      【解决方案3】:

      处理所有这些东西的一个好方法是使用变量, 例如,如果您定义如下内容:

      $buttons-hover-color: #somecolor;
      

      然后您可以在任何按钮中调用它,如果将来您想更改它,只需更改变量,所有按钮都会更新。 另一个重要的事情是,extend 在媒体查询中不起作用,所以将来当您针对移动设备时,您可能不希望在移动设备中出现红色,因此您只需要为其创建另一个 var。

      类似这样的:

      $btn-hover-color: red;
      $btn-hover-mobile: black;
      
      .button1 {
       background: black;
       padding: 5px 10px;
       text-decoration: none;
       color: white;
       &:hover {
        background-color: $btn-hover-color;
       }
      }
      
      .button2 {
        background: blue;
        padding: 5px 10px;
        text-decoration: none;
        color: white;
        &:hover {
          background-color: $btn-hover-color;
        }
      
        @media (max-width: 800px) {
          &:hover {
            background-color: $btn-hover-mobile;
          }
        }
       }
      

      希望对你有帮助

      【讨论】:

      • 这不是这里要问的。
      • 我问的是扩展
      【解决方案4】:

      您可以使用占位符。

      %hover {
        background: red;
      }
      
      .button1 {
        background: black;
        padding: 5px 10px;
        text-decoration: none;
        color: white;
        &:hover {
          @extend %hover;
        }
      }
      
      .button2 {
        background: blue;
        padding: 5px 10px;
        text-decoration: none;
        color: white;
        &:hover {
          @extend %hover;
        }
      }
      

      或者,也许是更好的解决方案,为您的按钮使用更通用的类:

      .btn {
        padding: 5px 10px;
        text-decoration: none;
        color: white;
      
        &:hover {
          background: red;
        }
      }
      
      .btn--1 {
        background: black;
      }
      
      .btn--2 {
        background: blue;
      }
      

      【讨论】:

      • 在 .button2 的悬停中添加 @extend .button1 也有效。方法对吗?
      • 它有效,但不是更好的解决方案,因为它很难阅读并返回到您正在扩展的样式。
      • 谢谢丹尼斯...您的解决方案更好并且符合标准:)
      • 是否可以返回类名?
      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 2018-02-17
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多