-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
43 lines (40 loc) · 1.02 KB
/
test.js
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
function normalizeStyle(value) {
if (typeof value !== 'object' || Array.isArray(value)) {
throw new Error('style must be an object')
}
let res = ''
function camelToKebab(str) {
return str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
}
function camelStyleToKebab(value) {
const res = {}
// 检查是否有浏览器支持的样式属性
const dom = document.createElement('div')
for (const key in value) {
if (key in dom.style) {
res[camelToKebab(key)] = value[key]
} else {
console.warn(`Invalid style property: ${key}`)
}
}
return res
}
let styleObj = {}
styleObj = camelStyleToKebab(value)
function transformStyleToString(res) {
let styleStr = ''
for (const key in res) {
styleStr += `${key}:${res[key]};`
}
return styleStr
}
res = transformStyleToString(styleObj)
return res
}
const style = {
color: 'red',
transform: 'translateX(10px)',
width: '100px',
height: '100px',
borderRadius: '5px',
}