在伪元素的“content:”属性中使用CSS变量(自定义属性)

前端之家收集整理的这篇文章主要介绍了在伪元素的“content:”属性中使用CSS变量(自定义属性)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
示例使用(我想要的)
div::after {
  content: var(--mouse-x) ' / ' var(--mouse-y);
}

显示它不工作的测试用例:

CodePen: CSS Variables in Pseudo Element’s “content:” Property (a test case) by Jase Smith

document.addEventListener('mousemove',(e) => {
  document.documentElement.style.setProperty('--mouse-x',e.clientX)
  document.documentElement.style.setProperty('--mouse-y',e.clientY)
  
  // output for explanation text
  document.querySelector('.x').innerHTML = e.clientX
  document.querySelector('.y').innerHTML = e.clientY
})
/* what I want!! */
div::after {
  content: var(--mouse-x,245)" / " var(--mouse-y,327);
}

/* setup and presentation styles */
div::before {
  content: 'mouse position:';
}
div {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(calc(var(--mouse-x,245) * 1px),calc(var(--mouse-y,327) * 1px));
  width: 10em;
  height: 10em;
  background: #ff3b80;
  color: #fff;
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
  border-radius: 100%;
  will-change: transform;
}
body {
  margin: 2em;
  font-family: sans-serif;
}
p {
  max-width: 50%;
  min-width: 25em;
}
<!-- test case: element with pseudo element -->
<div></div>

<!-- explanation (not test case) -->
<main>
  <pre><code>div::after {
  content: var(--mouse-x) ' / ' var(--mouse-y);
}</code></pre>
  <h1>If this worked...</h1>
  <p>
    We should see something like this: <b><span class="x">245</span> / <span class="y">327</span></b> updating with the mousemove coordinates inside the pseudo <i>::after</i> element for the div.
  </p>
</main>

解决方法

使用涉及 CSS Counters的黑客工作了.享受.
div::after {
  counter-reset: mouse-x var(--mouse-x,245) mouse-y var(--mouse-y,245);
  content: counter(mouse-x) " / " counter(mouse-y);
}

完整的代码

document.addEventListener('mousemove',e.clientY)
  
  // output for explanation text
  document.querySelector('.x').innerHTML = e.clientX
  document.querySelector('.y').innerHTML = e.clientY
})
/* what I want!! */
div::after {
  counter-reset: mouse-x var(--mouse-x,245);
  content: counter(mouse-x) " / " counter(mouse-y);
}

/* setup and presentation styles */
div::before {
  content: 'mouse position:';
}
div {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(calc(var(--mouse-x,327) * 1px));
  width: 10em;
  height: 10em;
  background: #ff3b80;
  color: #fff;
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
  border-radius: 100%;
  will-change: transform;
}
body {
  margin: 2em;
  font-family: sans-serif;
}
p {
  max-width: 50%;
  min-width: 25em;
}
<!-- test case: element with pseudo element -->
<div></div>

<!-- explanation (not test case) -->
<main>
  <pre><code>div::after {
  content: var(--mouse-x) ' / ' var(--mouse-y);
}</code></pre>
  <h1>If this worked...</h1>
  <p>
    We should see something like this: <b><span class="x">245</span> / <span class="y">327</span></b> updating with the mousemove coordinates inside the pseudo <i>::after</i> element for the div.
  </p>
</main>

猜你在找的CSS相关文章