【问题标题】:CSS counter not incrementing in shadow DOM [duplicate]CSS计数器在影子DOM中不增加[重复]
【发布时间】:2019-08-22 22:52:25
【问题描述】:

我有以下设置,其中 CSS 计数器适用于开槽内容,但 在影子 DOM 中。

import { LitElement, css, html } from 'lit-element';

class MyElement extends LitElement {

 static get properties() {
    return {
      counter: { type: Number },
    };
  }

  render() {
    return html`
      <div><slot></slot></div>
      <div class="foo">
        <h1>Hey</h1>
        <h1>Ho</h1>
      </div>
    `;
  }
}

MyElement.styles = [
  css`
    :host {
      counter-reset: partCounter afterCounter;
    }
    :host ::slotted(*):before {
      counter-increment: partCounter;
      content: 'Slotted ' counter(partCounter) '. ';
    }
    h1:after {
      counter-increment: afterCounter;
      content: ' Shadow ' counter(afterCounter) '. ';
    }
  `,
];
customElements.define('my-element', MyElement);
<my-element>
  <h1>one</h1>
  <h1>two</h1>
</my-element>

我看到这个输出:Shadow 1 Shadow 1。预期输出:Shadow 1 Shadow 2

为什么会这样?我对为什么的解释更感兴趣,尽管解决方案也很好。

Codesandbox 上的工作演示:https://codesandbox.io/s/4j6n7xwmj7

P.S.:这个 Github 线程中的一些提示,但对我而言,它表明它实际上应该可以工作:https://github.com/w3c/csswg-drafts/issues/2679

【问题讨论】:

    标签: html css web-component custom-component shadow-dom


    【解决方案1】:

    这一切都在你放置counter-reset的地方。

    :host 需要用于插槽内的东西,在这种情况下,我将另一个添加到 .foo

    您可以从下面的示例中看到它运行良好。

    是的,我把 LIT 都去掉了,但是有没有 LIT 的原理都是一样的。

    class MyElement extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({mode:'open'}).innerHTML = `
          <style>
          :host {
            counter-reset: partCounter;
          }
          :host ::slotted(*):before {
            counter-increment: partCounter;
            content: 'Slotted ' counter(partCounter) ': ';
          }
          .foo {
            counter-reset: afterCounter;
          }
          h1:before {
            counter-increment: afterCounter;
            content: ' Shadow ' counter(afterCounter) ' - ';
          }
          </style>
          <div><slot></slot></div>
          <div class="foo">
            <h1>Hey</h1>
            <h1>Ho</h1>
          </div>
        `;
      }
    }
    
    customElements.define('my-element', MyElement);
    <my-element>
      <h1>one</h1>
      <h1>two</h1>
    </my-element>

    为了看到每个都独立工作,我将其更改为以下内容:

    class MyElement extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({mode:'open'}).innerHTML = `
          <style>
          :host {
            counter-reset: partCounter -10;
          }
          :host ::slotted(*):before {
            counter-increment: partCounter;
            content: 'Slotted ' counter(partCounter) ': ';
          }
          .foo {
            counter-reset: afterCounter 30;
          }
          h1:before {
            counter-increment: afterCounter;
            content: ' Shadow ' counter(afterCounter) ' - ';
          }
          </style>
          <div><slot></slot></div>
          <div class="foo">
            <h1>Hey</h1>
            <h1>Ho</h1>
          </div>
        `;
      }
    }
    
    customElements.define('my-element', MyElement);
    <my-element>
      <h1>one</h1>
      <h1>two</h1>
    </my-element>

    【讨论】:

    • 感谢您的修复!一个很好的解释为什么出现在重复的问题中:原来:host 选择了影子 DOM 主机,它“离开”了影子 DOM:对我不好。我需要将我的 counter-reset 放入 shadow DOM 内的选择器中。
    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多