? 仅使用 CSS 选择第二行中的元素(特定于断点)
无论媒体查询断点如何,都无法选择第二行中的元素,因此您必须为每个断点为该行中的元素创建选择器。
您可以使用两个:nth-child pseudo-classes 来选择一系列元素。例如:
li:nth-child(n + 7):nth-child(-n + 12)
将选择元素 7 到 12,两者都包括在内。也就是说,如果您有 6 列,则为第二行。
img {
max-width:100%;
height:auto;
}
ul {
max-width:1200px;
list-style:none;
margin:0 auto;
padding:0;
display:flex;
flex-wrap:wrap;
}
li {
width:12%;
font-size: 0;
}
@media (min-width: 721px) {
li:nth-child(n+9):nth-child(-n+16) {
margin: 4px 0;
padding: 16px 0;
background: cyan;
}
}
@media (max-width: 720px) {
li {
width: 16%;
}
li:nth-child(n+7):nth-child(-n+12) {
margin: 4px 0;
padding: 16px 0;
background: red;
}
}
@media (max-width: 480px) {
li {
width: 22%;
}
li:nth-child(n+5):nth-child(-n+8) {
margin: 4px 0;
padding: 16px 0;
background: yellow;
}
}
<ul>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
</ul>
?用JS选择第二行的元素(断点无关)
如果您确实需要一个不依赖于每个断点的特定代码的通用解决方案,您可以使用jQuery 或普通JavaScript 根据它们的位置选择第二行中的元素并将它们添加到一个类.
基本上,您将它们全部迭代,每次.offsetTop 的值发生变化时,您只需进入下一行。
请注意,您必须收听 resize event 才能更新所选元素,以防不同断点处于活动状态并且列数已更改:
function styleSecondRow() {
let row = -1;
let currentTop = -1;
$('#grid > li').each(function() {
let top = this.offsetTop;
if (top > currentTop) {
// We stepped into the next row:
currentTop = top;
++row;
}
if (row === 1) {
// Second row:
this.classList.add('second-row');
} else {
// Remove .second-row from other rows:
this.classList.remove('second-row');
}
});
}
// Update second row styling if window is resized:
$(window).resize(function() {
styleSecondRow();
});
// Initialize second row styling for the firss time:
styleSecondRow();
img {
max-width:100%;
height:auto;
}
ul {
max-width:1200px;
list-style:none;
margin:0 auto;
padding:0;
display:flex;
flex-wrap:wrap;
}
li {
width:12%;
font-size: 0;
}
.second-row {
margin: 4px 0;
padding: 16px 0;
background: cyan;
}
@media (max-width: 720px) {
li {
width: 16%;
}
}
@media (max-width: 480px) {
li {
width: 22%;
}
}
<ul id="grid">
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
<li><img src="http://placehold.it/300x300"></li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
? 使用 CSS 选择任意偶数行中的元素
如果你想选择任何偶数行中的元素,你需要为每个断点设置多个选择器,正如@JacobDesight 建议的那样。例如,如果你有 8 列,你会这样做:
@media (...) {
li {
width: 12%;
}
li:nth-child(16n + 9),
li:nth-child(16n + 10),
li:nth-child(16n + 11),
li:nth-child(16n + 12),
li:nth-child(16n + 13),
li:nth-child(16n + 14),
li:nth-child(16n + 15),
li:nth-child(16n + 16) {
background: pink;
...
}
}
请注意,您有多个规则,但只有一个属性块。
? 使用 SCSS 选择任意偶数行中的元素,避免重复代码
同样使用SCSS,如果您暂时忘记媒体查询,您通常会使用@mixin、@include、@for和placeholder selector:
// Placeholder selector that we will extend to avoid duplicating
// the properties for each generated nth-child selector:
%even-row { background: pink; }
// Mixin to avoid duplicating the @for block multiple times.
// It takes the number of columns as a parameter:
@mixin even-rows-mixin($columns) {
@for $i from 1 through $columns {
&:nth-child(#{2 * $columns}n + #{$i + $columns}) {
@extend %even-row;
}
}
}
li {
@include even-rows-mixin(8);
width: 12%;
}
这将生成以下代码:
li:nth-child(16n + 9),
li:nth-child(16n + 10),
li:nth-child(16n + 11),
li:nth-child(16n + 12),
li:nth-child(16n + 13),
li:nth-child(16n + 14),
li:nth-child(16n + 15),
li:nth-child(16n + 16) {
background: pink;
}
li {
width: 12%;
}
但是,您不能在媒体查询中使用@extend。
如果您删除@extend,您最终会得到类似于@GCyrillus 建议的内容。您可以将其包装在 mixin 中以避免在每个媒体查询中重复 @for 块,但这仍会生成重复的代码。 mixin 将如下所示:
@mixin even-rows-mixin($columns) {
@for $i from 1 through $columns {
&:nth-child(#{2 * $columns}n + #{$i + $columns}) {
background: pink;
}
}
}
它将生成$columns 选择器和重复属性块(在此示例中仅background),如果您使用此mixin 过多或向其添加更多属性,这将使您的样式表快速增长。
编辑:我怀疑这可以使用插值来完成,但不确定,所以我打开了另一个问题,有人来救援:SCSS @extend inside media query alternatives to avoid duplicate properties
基本上,您应该在 mixin 中构建一个选择器变量并使用插值来创建具有一组固定属性的规则,或者如果您更喜欢动态设置它们,请使用@content:
@mixin even-rows-mixin($columns, $rule) {
$selector : '';
@for $i from 1 through $columns {
$selector: $selector + $rule + '('+ #{2 * $columns}n + ' + ' + #{$i + $columns} + '),';
}
#{$selector} {
@content; // Or just add your properties here.
}
}
...
@media(...) {
...
@include even-rows-mixin(8, 'li:nth-child') {
background: pink;
};
...
}
请注意,此代码不能在 CodePen 中编译,但在使用 node-sass 或其他在线编译器时可以编译:http://beautifytools.com/scss-compiler.php