扩展&:将任何班级悬停在任何其他班级的SASS或SCSS中

Ome*_*mer 2 html css sass hover

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

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

HTML

<a href="#" class="button1">Button 1</a>
<a href="#" class="button2">Button 2</a>
Run Code Online (Sandbox Code Playgroud)

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
  }
}
Run Code Online (Sandbox Code Playgroud)

Den*_*ato 8

您可以使用占位符.

%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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

.btn {
  padding: 5px 10px;
  text-decoration: none;
  color: white;

  &:hover {
    background: red;
  }
}

.btn--1 {
  background: black;
}

.btn--2 {
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)