html - CSS 计数器不会在影子 DOM 中递增

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

<分区>

我有以下设置,其中 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

最佳答案

这一切都在你放置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>

关于html - CSS 计数器不会在影子 DOM 中递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55460772/

上一篇:html - 如何将可点击图标放置在 flexbox 网格中的(右下角)图像上?

下一篇:css - 响应棋盘图案背景

相关文章:

javascript - 预加载一个 DIV

javascript - Ionic 4 中的 Shadow DOM/Web 组件样式

javascript - 根据数据属性显示/隐藏带有 id 的 div (onclick)

javascript - 在 html5 上检查在线状态的最佳方式

html - 跨度内的 CSS 样式异常?

html - 基本 CSS 帮助 - 删除形状

javascript - 无法获取目标元素

javascript - 使用 JavaScript 验证表单

angular - 是否可以将 Angular 4 应用程序包装在 Web 组件中?

javascript - 将 GIF 播放器 Web 组件转换为 React 组件?