KeyframeEffect: pseudoElement 屬性
KeyframeEffect 介面的 pseudoElement 屬性是一個字串,表示正在動畫的偽元素。對於不以偽元素為目標的動畫,它可能為 null。它同時充當 getter 和 setter,但 CSS 生成的動畫和過渡除外。
注意:如果將其設定為舊的單冒號語法,如 :before、:after、:first-letter 或 :first-line,該字串將被轉換為其雙冒號現代版本(分別為 ::before、::after、::first-letter 和 ::first-line)。
值
字串或 null。
異常
SyntaxErrorDOMException-
嘗試將此屬性設定為元素、無效偽元素(不存在或拼寫錯誤)時丟擲。然後該屬性將保持不變。
示例
html
<div id="text">Some text</div>
<pre id="log"></pre>
css
#text::after {
content: "👹";
display: inline-block; /* Needed as the `transform` property does not apply to inline elements */
font-size: 2rem;
}
#text::before {
content: "🤠";
display: inline-block;
font-size: 2rem;
}
js
const log = document.getElementById("log");
const text = document.getElementById("text");
// Create the keyframe and launch the animation
const animation = text.animate([{ transform: "rotate(360deg)" }], {
duration: 3000,
iterations: Infinity,
pseudoElement: "::after",
});
// Get the value of KeyframeEffect.pseudoElement
function logPseudoElement() {
const keyframeEffect = animation.effect;
log.textContent = `Value of pseudoElement animated: ${keyframeEffect.pseudoElement}`;
requestAnimationFrame(logPseudoElement);
}
// Every 6 seconds, switch the pseudo-element animated
function switchPseudoElement() {
const keyframeEffect = animation.effect;
keyframeEffect.pseudoElement =
keyframeEffect.pseudoElement === "::after" ? "::before" : "::after";
setTimeout(switchPseudoElement, 6000);
}
switchPseudoElement();
logPseudoElement();
規範
| 規範 |
|---|
| Web 動畫 # dom-keyframeeffect-pseudoelement |
瀏覽器相容性
載入中…
另見
- Web Animations API
KeyframeEffect介面KeyframeEffect()建構函式target屬性