-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathat-property.html
108 lines (99 loc) · 2.71 KB
/
at-property.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Houdini @property demo</title>
<style>
header {
grid-column: span 2;
}
h1 {
text-align: center;
}
.box {
display: grid;
grid-template-columns: min-content 360px;
grid-template-rows: max-content 1fr;
justify-content: center;
}
#style {
border: 1px solid #999;
font-size: 16px;
border-radius: 10px;
padding-right: 1em;
}
#error {
color: red;
}
</style>
</head>
<body>
<div class="box">
<header>
<h1>CSS Houdini @property Demo</h1>
<p>@property 是 JS CSS.registerProperty() 方法在 CSS 中的实现,通过它可以使自定义属性产生过渡或动画。<a href="https://www.chromestatus.com/feature/5193698449031168" target="_blank">Chrome 85 + 开始正式支持。</a></p>
<p id="error"></p>
<ul>
<!-- <li><code>open /Applications/Google\ Chrome\ Canary.app/ --args --enable-blink-features=CSSVariables2AtProperty</code></li> -->
<li><a href="https://bugs.chromium.org/p/chromium/issues/detail?id=973830" target="_blank">Chromium Bug 973830</a></li>
<li><a href="https://drafts.css-houdini.org/css-properties-values-api/#at-property-rule" target="_blank">Spec</a></li>
</ul>
</header>
<pre>
<style id="style" contenteditable style="display: block;">
@property --size {
syntax: "<length>";
inherits: false;
initial-value: 150px;
}
.test {
width: var(--size);
height: var(--size);
transition: --size .2s;
background: lightseagreen;
border: 2px solid #2d563c;
border-radius: 50%;
place-self: center;
}
.test:hover {
--size: 300px;
}
</style>
</pre>
<div class="test"></div>
</div>
<script>
// 判断是否支持 @property
let el = document.createElement('style')
el.textContent = `
@property --foo {
syntax: "<length>";
inherits: false;
initial-value: 10px;
}
`
document.documentElement.appendChild(el)
function isSupportedAtRule(el) {
if (el.sheet.cssRules.length > 0) {
return el.sheet.cssRules[0].cssText.indexOf('initial-value') != -1
} else {
return false
}
}
const error = document.getElementById('error')
if (!isSupportedAtRule(el)) {
error.textContent = '当前浏览器不支持 @property, 请确保 Chrome 版本为 85+'
}
// 使用 JS 方法注册自定义属性
/* if ('registerProperty' in CSS) {
CSS.registerProperty({
name: '--size',
syntax: '<length>',
initialValue: '100px',
inherits: false,
});
} */
</script>
</body>
</html>