【问题标题】:How to re-calculate a style on sibling elements?如何重新计算兄弟元素的样式?
【发布时间】:2021-06-01 01:35:15
【问题描述】:

不确定我是否正确地问了这个问题。所以请允许我进一步解释。

HTML

<div class='col'>
  <div id='q1' class='qBlock'
</div>
<div class='col'>
  <div id='q2' class='qBlock'
</div>
<div class='col'>
  <div id='q3' class='qBlock'
</div>

@qBlockBG: #ccc;
.qBlock { color: white; }
#q1: { background-color: @qBlockBG; }
#q{n}: { background-color: darken(#q{n-1}, 10%); }    <--- doesn't work, but that's the idea

我想要做的是为每个兄弟姐妹重新计算#q1 的基本样式,以便每个额外兄弟姐妹的颜色更深。所以#q2#q1 暗10%,#q3#q2 暗10%,以此类推。

【问题讨论】:

    标签: css loops for-loop css-selectors less


    【解决方案1】:

    您可以使用带有保护表达式的递归 mixin 在 LESS 中模拟循环。以下是适用于您的案例的示例:

    #q1 {
      background-color: #ccc;
    }
    
    /* recursive mixin with guard expression - condition */
    .darker-qs(@color, @index) when (@index < 10) { 
    
      @darker-color: darken(@color, 10%);
    
      /* the statement */
      #q@{index} {
        background-color: @darker-color;
      }
      /* end of the statement */
    
      /* the next iteration's call - final expression */
      .darker-qs(@darker-color, @index + 1); 
    }
    
    /* the primary call - initialization */
    .darker-qs(#ccc, 2); 
    

    【讨论】:

    • 太棒了!谢谢。简洁明了……适用! 10/10 芽。
    • 这也帮助我更好地理解了循环。 2换1!再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2017-11-06
    • 2012-01-22
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多