【问题标题】:target first, even/odd and last element with class, without wrapper, among other elements (nth-of-class behaviour) :)在其他元素中定位第一个,偶数/奇数和最后一个元素,没有包装器(第n个类行为):)
【发布时间】:2017-03-24 19:51:55
【问题描述】:

我有以下结构:

<div class="elementWrapper">
  <div>1. Element</div>
  <div>2. Element</div>
  <div>3. Element</div>
  <div class="alternate">1. Element with spec. Class</div>
  <div class="alternate">2. Element with spec. Class</div>
  <div class="alternate">3. Element with spec. Class</div>
  <div class="alternate">4. Element with spec. Class</div>
  <div class="alternate">5. Element with spec. Class</div>
  <div>9. Element</div>
  <div>10. Element</div>
  <div>11. Element</div>
</div>

之前和之后可能有未知数量的多个元素,我无法使用 class="alternate" 在元素周围添加“包装”div。 (一切都会好的)。

我想给第一个 .alternate 元素一个边框顶部,最后一个 .alternate 元素一个边框底部。并且所有 .alternate 元素的每一行都应该有不同的背景颜色(偶数/奇数)。

我以不同的方式尝试过,我知道 nth-of-type 和 nth-child 不起作用,因为我的 .alternate 元素周围没有包装 div,所以它不起作用,因为所有元素都计算为偶数/奇数等。

于是我就这个问题和可能的解决方案做了一支笔:

http://codepen.io/emjay/pen/RpyyOo

我想问你,最好的方法是什么——不改变结构——。有没有唯一可行的 CSS 解决方案?

感谢您的帮助!

【问题讨论】:

    标签: css


    【解决方案1】:

    对于第一个问题(将border-top 添加到第一个.alternate 元素和border-bottom 到最后一个.alternate 元素),您可以通过单独添加border-top 来实现:

    • 紧跟在 :not() 的元素之后的第一个 .alternate 元素具有 .alternate 类,并且,
    • 也到第一个没有.alternate类的元素紧跟在一个元素之后。

    如果第一个.alternate 元素之前没有元素,您还需要将border-top 添加到同样是:first-child 的:first-child 元素中,并且如果没有最后一个.alternate 之后的元素,您需要将border-bottom 添加到.alternate 元素,这也是:last-child。

    对于关于“斑马条纹”.alternate 元素的第二个问题,假设奇数或偶数元素是否具有备用background 无关紧要,您可以通过简单的:nth-of-type()(或:nth-child())来实现伪类。但是,如果您要求第一个 .alternate 元素始终具有相同的 background 而不管它前面的元素数量,您将需要求助于 JavaScript - 可能仅使用 CSS但需要 荒谬 数量的选择器(参见 this answer 作为示例)。

    (function(){
      var wrappers=document.querySelectorAll(".elementWrapper"),
          x=wrappers.length,
          divs,y,alt;
      while(x--){
        divs=wrappers[x].querySelectorAll(".alternate");
        y=divs.length;
        alt=!(y%2);
        while(y--)
          divs[y].classList.add((alt=!alt)?"odd":"even");
      }
    })();
    /** JQuery **/
    //$('.alternate:odd').addClass('odd')
    //$('.alternate:even').addClass('even');
    .elementWrapper>div:not(.alternate)+div.alternate,
    .elementWrapper>div.alternate+div:not(.alternate),
    .elementWrapper>div.alternate:first-child{
      border-top:1px solid #000;
    }
    .elementWrapper>div.alternate:last-child{
      border-bottom:1px solid #000;
    }
    .elementWrapper>div.alternate.odd{
      background:#ccc;
    }
    .elementWrapper>div.alternate.even{
      background:#eee;
    }
    
    /** Uncomment below for CSS-only zebra-striping **/
    /*.elementWrapper>div.alternate:nth-of-type(odd){
      background:#ccc;
    }
    .elementWrapper>div.alternate:nth-of-type(even){
      background:#eee;
    }*/
    
    /** "housekeeping" **/.elementWrapper{background:#fff;color:#000;margin:0 0 20px;}.elementWrapper>div{font-family:sans-serif;font-size:14px;overflow:hidden;padding:5px;text-overflow:ellipsis;white-space:nowrap;}
    <div class="elementWrapper">
      <div>1. Element</div>
      <div class="alternate">1. Element with spec. Class</div>
      <div class="alternate">2. Element with spec. Class</div>
      <div class="alternate">3. Element with spec. Class</div>
      <div class="alternate">4. Element with spec. Class</div>
      <div class="alternate">5. Element with spec. Class</div>
      <div>9. Element</div>
    </div>
    <div class="elementWrapper">
      <div>1. Element</div>
      <div>2. Element</div>
      <div class="alternate">1. Element with spec. Class</div>
      <div class="alternate">2. Element with spec. Class</div>
      <div class="alternate">3. Element with spec. Class</div>
      <div class="alternate">4. Element with spec. Class</div>
    </div>

    【讨论】:

    • 看起来不错,谢谢。而是一个补充。如果在 Elements with class alternate 之前或之后没有元素,则该行将不起作用。我认为这将是解决方案:codepen.io/emjay/pen/aJGjZw也许你可以改变你的答案:)
    • 好的,另一个问题。偶数/奇数没有像预期的那样工作。因为它不仅针对.alternate.elementWrapper 中的所有元素。请看这里:codepen.io/emjay/pen/GWdXjJ
    • 请参阅我的回答中的警告:“假设奇数或偶数元素是否具有备用 background 无关紧要”。但是,如果您给我几分钟的时间,我相信我可以想出一个解决方案,这样第一个 .alternate 元素将始终具有相同的 background,而不管它前面的元素数量如何。
    • 那太好了,你有你需要的所有时间。 :)
    • 我可能很快就谈过了!我假设.alternate 元素的数量是可变的?是否存在最大数量?
    【解决方案2】:

    基于@Shaggy 的回答,使用:not 和nth-of-type 选择器,您可以在纯css 中呈现所需的内容。

    参考代码:

    .elementWrapper div:not(.alternate)+div.alternate,
    .elementWrapper div.alternate+div:not(.alternate) {
      border-top: 2px solid blue;
    }
    
    .elementWrapper div.alternate:nth-of-type(odd) {
      background: green;
    }
    
    .elementWrapper div.alternate:nth-of-type(even) {
      background: red;
    }
    <div class="elementWrapper">
      <div>1. Element</div>
      <div>2. Element</div>
      <div>3. Element</div>
      <div class="alternate">1. Element with spec. Class</div>
      <div class="alternate">2. Element with spec. Class</div>
      <div class="alternate">3. Element with spec. Class</div>
      <div class="alternate">4. Element with spec. Class</div>
      <div class="alternate">5. Element with spec. Class</div>
      <div>9. Element</div>
      <div>10. Element</div>
      <div>11. Element</div>
    </div>

    正如 OP 所指出的,如果 div 之前或之后没有 .alternate 元素,则上述解决方案将不起作用。那么first-child 和last-child 的用法将是首选。

    /* if there are no elements before .alternate */
    .elementWrapper > div.alternate:first-child{
      border-top: 2px solid blue;
    }
    
    /* if there are no elements after .alternate */
    .elementWrapper > div.alternate:last-child{
      border-bottom: 2px solid blue;
    }
    

    【讨论】:

    • 看起来不错,谢谢。而是一个补充。如果在 Elements with class alternate 之前或之后没有元素,则该行将不起作用。我认为这将是解决方案:codepen.io/emjay/pen/aJGjZw 也许你可以改变你的答案:)
    • 修改了我的答案。 :)
    • 这与我的解决方案有何不同?
    • 好的,另一个问题。偶数/奇数没有像预期的那样工作。因为它不仅针对.alternate .elementWrapper 中的所有元素。请看这里:codepen.io/emjay/pen/GWdXjJ
    【解决方案3】:

    如果您的 .alternate 元素集中在一个区域(也就是说,它们都在一起),您可以使用以下样式获得斑马样式:

    .alternate {
      background: gray;
    }
    
    div:nth-of-type(odd):not(.alternate)+div.alternate~div.alternate:nth-of-type(even),
    div:nth-of-type(odd):not(.alternate)+.alternate:nth-of-type(even),
    div:nth-of-type(even):not(.alternate)+div.alternate~div.alternate:nth-of-type(odd),
    div:nth-of-type(even):not(.alternate)+.alternate:nth-of-type(odd) {
      background: red;
    }
    <div class="wrap">
      <div class="col">
        <div class="elementWrapper">
          <div>1. Element</div>
          <div>2. Element</div>
          <div>3. Element</div>
          <div class="alternate">1. Element with spec. Class</div>
          <div class="alternate">2. Element with spec. Class</div>
          <div class="alternate">3. Element with spec. Class</div>
          <div class="alternate">4. Element with spec. Class</div>
          <div class="alternate">5. Element with spec. Class</div>
          <div>9. Element</div>
          <div>10. Element</div>
          <div>11. Element</div>
        </div>
      </div>
      <div class="col">
        <div class="elementWrapper">
          <div>1. Element</div>
          <div>2. Element</div>
          <div>3. Element</div>
          <div>4. Element</div>
          <div class="alternate">1. Element with spec. Class</div>
          <div class="alternate">2. Element with spec. Class</div>
          <div class="alternate">3. Element with spec. Class</div>
          <div class="alternate">4. Element with spec. Class</div>
          <div class="alternate">5. Element with spec. Class</div>
          <div>10. Element</div>
          <div>11. Element</div>
          <div>12. Element</div>
        </div>
      </div>
    </div>

    【讨论】:

    • 很好 :) 这也是我要走的路线,但认为它过于复杂。为了完整起见,我建议更新您的答案以解决边界问题。
    • 顺便说一下,您可以将第一条规则的选择器简化为 div.alternate。
    • 谢谢!我已按照您的建议编辑了 sn-p。我不认为我的回答是完整的,而是对您回答的补充,很好地涵盖了边界问题
    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2019-09-16
    • 1970-01-01
    • 2010-09-20
    • 2013-01-14
    相关资源
    最近更新 更多