我有一个清单的项目,我正在用挠曲。它们被堆放在一排水平的地方。
我想知道是否有可能编写一个if/ the语句,它基本上可以执行以下操作:
如果列表中有4个或更少的项目,请使用“对齐-内容:间隔”,但如果有5个或更多项,则使用“对齐-内容:空间-均匀”。
我很难理解如何编写这样的东西来测试它,并且在Sass文档中找不到涵盖这种情况的任何东西--这样的事情可能吗?
发布于 2017-10-27 13:43:32
由于不能使用SASS来计数元素,所以可以使用CSS根据父级子项的数量(当/如果我们获得父选择器时,我们也可以针对父元素:)
基于这个解决方案,Can CSS detect the number of children an element has?,如果有5个或更多,我们可以将nth-child修改为所有目标。
在您想要更改justify-content属性的特定情况下,这不会有帮助,因为该属性是在父属性上设置的,尽管还有其他方法可以在flex项之间创建空间,即自动边距,可以在其中使用此方法。
堆栈段
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
background: blue;
height: 20px;
margin: 5px;
}
/* five or more items */
li:first-child:nth-last-child(n+5),
li:first-child:nth-last-child(n+5) ~ li {
background: red;
}<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
如果这样做没有帮助,那么使用一个小脚本来向父类添加一个类,如果它的子级大于4个。
堆栈段
$('ul').each(function(idx, el) {
if(el.children.length > 4) {
$(el).addClass('more_than_4');
}
});ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
background: blue;
height: 20px;
margin: 5px;
}
ul.more_than_4 {
background: red;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
发布于 2017-10-27 09:29:07
下面是在SASS中编写if/ the语句的语法:
$var: true !default;
@if $var == true {
// Conditional code
}
$other: single;
@if $other == single {
// Code for if it’s single
} @else if $other == double {
// Double code
} @else {
// Default if it’s neither
}@if是一个控制指令,类似于@for, @each and @while。
https://stackoverflow.com/questions/46971097
复制相似问题