0

我有两个嵌套模块:

<div class="header">
.....
<i class="topIcon"></i>
......
</div>

我有两个单独的文件(我想将其分开,因为它们是可重用的部分,可以在整个应用程序中单独使用)充当 SMACSS 术语中的模块:
标题:

.header {
   /* header styles */
}

图标:

.topIcon {
 /* icon styles */
} 

现在我想对我的topIconwhen headerhas :hoverstate 应用一些样式。
我可以放入.header:hover .topIcon我的图标模块文件并应用样式,但是从我的 POV 来看,它违反了 SMACSS 规则。

你有更好的建议吗?

4

1 回答 1

1

我过去常常使用Sass' & 选择器来做到这一点:

.topIcon {
  /* icon styles */

  .header & {
    /* modified styles when it's in header */
  }
  .header:hover & {
    /* modified styles when it's in header thats hovered */
  }
} 

结果将是

.topIcon {
  /* icon styles */
}
.header .topIcon {
  /* modified styles when it's in header */
}
.header:hover .topIcon {
  /* modified styles when it's in header thats hovered */
}

这样,我将与图标相关的样式保留在图标文件中,但避免在根级别使用“外来”类。

这种方式的一个弱点可能是,您可能在头文件中还有另一个 .header:hover 规则,这可能会让其他人感到困惑,在哪里放置什么。

于 2017-12-12T13:42:01.723 回答