CSS:如何有条件地选择最后X nth-last-of-type只有当他们中的任何一个有一个类?

46scxncf  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(90)

仅使用CSS,我想选择最后4个元素,但只有当它们中的任何一个具有“selected”类时。

  • 在片段中,你可以在第二行看到我希望它如何工作。*
let sel=0;
setTimeout(setSelected, 1000);

function setSelected() {
  document.getElementById("d"+sel).classList.remove("selected");
  document.getElementById("j"+sel).classList.remove("selected");

  if (++sel>5) sel = 0;
  document.getElementById("d"+sel).classList.add("selected");

  // This is only to show what i would like to get with CSS selectors
  document.getElementById("j"+sel).classList.add("selected");
  if (sel>=3) {
    document.getElementById("j3").classList.add("active");
    document.getElementById("j4").classList.add("active");
    document.getElementById("j5").classList.add("active");
  }else{
    document.getElementById("j3").classList.remove("active");
    document.getElementById("j4").classList.remove("active");
    document.getElementById("j5").classList.remove("active");
  }
  setTimeout(setSelected, 1000);
}
.container {text-align: center;}
.normal, .example {
  display: inline-block;
  width: 3rem;
  color: white;
  background-color: #004;
  text-align: center;
  border: 2px solid transparent;
}
.normal:last-of-type,
.normal:nth-last-of-type(2),
.normal:nth-last-of-type(3)
{
  background-color: #A00;
}
.selected{
  background-color: #0A0;
  border: 2px solid #0A0;
}
.active{background-color: #A00;}
<p>CSS Only</p>
<div class="container">
  <div id="d0" class="normal selected">0</div>
  <div id="d1" class="normal">1</div>
  <div id="d2" class="normal">2</div>
  <div id="d3" class="normal">3</div>
  <div id="d4" class="normal">4</div>
  <div id="d5" class="normal">5</div>
</div>
<p>example with Javascript</p>
<div class="container">
  <div id="j0" class="example selected">0</div>
  <div id="j1" class="example">1</div>
  <div id="j2" class="example">2</div>
  <div id="j3" class="example">3</div>
  <div id="j4" class="example">4</div>
  <div id="j5" class="example">5</div>
</div>

我开始选择最后一个:

.normal:last-of-type,
.normal:nth-last-of-type(2),
.normal:nth-last-of-type(3)
{
  background-color: #A00;
}


但后来我没能走得更远;通过阅读CSS :has()的描述,我有一个印象,它和nth-last-of-type()的巧妙组合可以让我到达那里,但我不知道如何构建选择器的“堆栈”。
是否有可能,只使用CSS,有条件地选择最后3个项目,只有当其中一个有“选定”类?如果是,如何?

  • 编辑-由于问题很接近,因为似乎是Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?的重复:
    我的问题是,当只有一个div匹配任意选择器时,我需要选择最后三个div中的ALL,而在链接问题中,所有选定的元素都匹配选择器。
    试着换一种方式说,当3或4或5具有“selected”类时,我需要选择元素3、4和5。
pkln4tw6

pkln4tw61#

你可以尝试.container:has(.selected:nth-last-of-type(-n + 3)) > :nth-last-of-type(-n + 3)。当容器中有一个.selected的元素,并且该元素是最后3个元素之一时,选择最后3个

let sel=0;
setTimeout(setSelected, 1000);

function setSelected() {
  document.getElementById("d"+sel).classList.remove("selected");
  document.getElementById("j"+sel).classList.remove("selected");

  if (++sel>5) sel = 0;
  document.getElementById("d"+sel).classList.add("selected");

  // This is only to show what i would like to get with CSS selectors
  document.getElementById("j"+sel).classList.add("selected");
  if (sel>=3) {
    document.getElementById("j3").classList.add("active");
    document.getElementById("j4").classList.add("active");
    document.getElementById("j5").classList.add("active");
  }else{
    document.getElementById("j3").classList.remove("active");
    document.getElementById("j4").classList.remove("active");
    document.getElementById("j5").classList.remove("active");
  }
  setTimeout(setSelected, 1000);
}
.container {text-align: center;}
.normal, .example {
  display: inline-block;
  width: 3rem;
  color: white;
  background-color: #004;
  text-align: center;
  border: 2px solid transparent;
}

.container:has(.selected:nth-last-of-type(-n + 3)) > :nth-last-of-type(-n + 3) {
  background-color: #A00;
}
.selected{
  background-color: #0A0;
  border: 2px solid #0A0;
}
.active{background-color: #A00;}
<p>CSS Only</p>
<div class="container">
  <div id="d0" class="normal selected">0</div>
  <div id="d1" class="normal">1</div>
  <div id="d2" class="normal">2</div>
  <div id="d3" class="normal">3</div>
  <div id="d4" class="normal">4</div>
  <div id="d5" class="normal">5</div>
</div>
<p>example with Javascript</p>
<div class="container">
  <div id="j0" class="example selected">0</div>
  <div id="j1" class="example">1</div>
  <div id="j2" class="example">2</div>
  <div id="j3" class="example">3</div>
  <div id="j4" class="example">4</div>
  <div id="j5" class="example">5</div>
</div>
polhcujo

polhcujo2#

它可以通过nth-last-of-type():has的组合来完成。

/* For demo. Set .selected on click */
const items = document.querySelectorAll(".container > div");
items.forEach(item => {
  item.addEventListener('click', onclick);
});

function onclick(){
    document.querySelector(".selected").classList.remove("selected")
   this.classList.add("selected");
}
.selected {
  outline:2px dotted;
}
/* Test if relational pseudo-classes are supported. At the time of writing Firefox is lagging behind. */
@supports selector(:has(*)) {
  /* 
  If the last nth is selected
  or HAS a subsequent sibling which is selected
  then highlight it and the siblings: 
  */
  .container div:nth-last-of-type(3):is(.selected),
  .container div:nth-last-of-type(3):has(~.selected),
  .container div:nth-last-of-type(3):is(.selected) ~ div,
  .container div:nth-last-of-type(3):has(~.selected) ~ div {
    background: green;
  }
}
click an item:
<div class="container">
  <div id="d0" class="normal selected">0</div>
  <div id="d1" class="normal">1</div>
  <div id="d2" class="normal">2</div>
  <div id="d3" class="normal">3</div>
  <div id="d4" class="normal">4</div>
  <div id="d5" class="normal">5</div>
</div>

相关问题