热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Sass&&and向类添加标记-SassAmpersandtoaddTagtoClass

IhavesomeSasslikethis:我有一些这样的蠢货:.zbounce1,.zbounce2,.zbounce3,.zbounce4animation-nam

I have some Sass like this:

我有一些这样的蠢货:

.zbounce1, .zbounce2, .zbounce3, .zbounce4
    animation-name: bounceIn
    animation-duration: .5s
    animation-timing-function: ease-in-out

span.zbounce1, span.zbounce2, span.zbounce3, span.zbounce4
    display: inline-block

I would like to simplyfy the second part to a nested bit of the first part, so I don't have to mention every class again:

我想把第二部分简化为第一部分的嵌套部分,这样我就不用再提每一个类了:

    span.&
        display: inline-block

Sadly, this is not legal. (The other way round, specializing a bunch of styles with a nested &.anotherClass is easy...

可悲的是,这是不合法的。(反过来说,用嵌套&专门化一组样式。anotherClass很容易…

Any workaround to achieve this? (perhaps with the @extendOnly-Placeholder?) or some funky sass concatenation...?

有什么解决办法吗?(也许是@extendOnly-Placeholder?)或者一些时髦的sass连接……?

2 个解决方案

#1


3  

You can achieve this in native sass, it's just a bit convoluted. You need to use selector-append to add the span to all of the selectors, and @at-root to do so at root level (if that makes sense? it's hard to even explain). Basically it looks like this:

您可以在本地sass中实现这一点,只是有点复杂。您需要使用selecter -append将span添加到所有选择器中,并在根级别上使用@at-root(如果这有意义的话)。这很难解释)。基本上是这样的:

.zbounce1, .zbounce2, .zbounce3, .zbounce4 {
  animation-name: bounceIn;
  animation-duration: .5s;
  animation-timing-function: ease-in-out;
  @at-root #{selector-append(span, &)} {
    display: inline-block;
  }
}

Which should output this result:

哪个应该输出这个结果:

.zbounce1, .zbounce2, .zbounce3, .zbounce4 {
  animation-name: bounceIn;
  animation-duration: .5s;
  animation-timing-function: ease-in-out;
}
span.zbounce1, span.zbounce2, span.zbounce3, span.zbounce4 {
  display: inline-block;
}

Here's a Codepen example (you'll have to view source and look for the style block in the header to see the output, I'm not sure how else to demonstrate it but hopefully this helps!)

这里有一个代码页示例(您必须查看源代码并在标题中查找样式块以查看输出,我不确定还有什么其他方法来演示它,但希望这能有所帮助!)

#2


1  

Ladies and gentlemen...

女士们,先生们…

.zbounce, .zbounce1, .zbounce2, .zbounce3, .zbounce4, .zbounce5
    ...
    @at-root #{selector-append(span, &)}
        display: inline-block

getting me, what I want:

得到我想要的:

span.zbounce, span.zbounce1, span.zbounce2, 
span.zbounce3, span.zbounce4, span.zbounce5 { 
    display: inline-block;
}

Thanks to @ovokuro for pointing me into the – right – direction!

感谢@ovokuro为我指出正确的方向!


推荐阅读
author-avatar
零开始1023_511
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有