【发布时间】:2014-03-29 19:47:43
【问题描述】:
我是 Sass 新手,想使用“if”语句来检测元素上的现有类,以生成相关的 css。
我的设置有大量 Javascript 生成的图像,它们被分配了一个唯一的 ID,以及一个“图片”类和一个随机分配的顶部、右侧、底部或左侧的类别。
我还在为 Sass 使用随机函数(可在此处找到:https://gist.github.com/chriseppstein/1561650),并希望为每个 ID 分配不同的值,以便随机定位每个元素。
我的 SCSS 有以下内容,根据分配的类别定位图像:
@for $i from 0 through $number-of-pictures {
#picture-#{$i}{
&.top {
left: random($stage-width);
}
&.right {
top: random($stage-height);
}
&.bottom {
left: random($stage-width);
}
&.left {
top: random($stage-height);
}
}
}
这很好用,但会创建大量未使用的声明块。例如,#picture-38 被分配了类“.top”,所以我只需要第一个声明块,但 CSS 会导出所有选项:
#picture-38.top {
left: 38px; //This is a random number that is different for each ID.
}
#picture-38.right {
top: 28px;
}
#picture-38.bottom {
left: 12px;
}
#picture-38.left {
top: 47px;
}
我需要的是一个 if 语句,它在解析 css 之前检测元素是否有一个类。比如:
@if "#picture-#{$i} has class $class-top" {
&.top {
left: random($stage-width);
}
}
有什么想法吗?
【问题讨论】:
-
我不相信这对于 SASS(或任何其他预处理器)是可能的。
标签: if-statement sass