KeyframeEffect: thuộc tính pseudoElement
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Thuộc tính pseudoElement của giao diện KeyframeEffect là một chuỗi đại diện cho phần tử giả đang được hoạt hình. Có thể là null cho các hoạt hình không nhắm mục tiêu vào phần tử giả. Nó hoạt động như cả getter lẫn setter, ngoại trừ với các hoạt hình và transition được tạo bởi CSS.
Note:
Nếu được đặt thành cú pháp dấu hai chấm đơn kế thừa của :before, :after, :first-letter, hoặc :first-line, chuỗi được chuyển đổi thành phiên bản dấu hai chấm đôi hiện đại (::before, ::after, ::first-letter, và ::first-line, tương ứng).
Giá trị
Một chuỗi hoặc null.
Ngoại lệ
SyntaxErrorDOMException-
Được ném khi cố gắng đặt thuộc tính này thành một phần tử, phần tử giả không hợp lệ (không tồn tại hoặc viết sai chính tả). Thuộc tính sau đó được giữ nguyên.
Ví dụ
<div id="text">Some text</div>
<pre id="log"></pre>
#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;
}
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();
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Web Animations> # dom-keyframeeffect-pseudoelement> |
Tương thích trình duyệt
Xem thêm
- Web Animations API
- Giao diện
KeyframeEffect - Hàm khởi tạo
KeyframeEffect() - Thuộc tính
target