-
Notifications
You must be signed in to change notification settings - Fork 31
/
step3.html
57 lines (45 loc) · 1.55 KB
/
step3.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Example For custome-elements</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<info-card name="Jarvis" avatar="https://avatars1.githubusercontent.com/u/9568094?v=4" />
<!-- P.S. 这里会触发一个Chrome67版本的一个隐藏bug -->
<script>
class InfoCard extends HTMLElement {
constructor() {
super()
let avatar = this.getAttribute('avatar')
let name = this.getAttribute('name')
initShadow(this, { avatar, name })
}
}
function initShadow($host, { avatar, name, isOpen }) {
let $shadow = $host.attachShadow({ mode: isOpen ? 'open' : 'closed' })
let $style = document.createElement('style')
let $wrap = document.createElement('div')
let $avatar = document.createElement('img')
let $name = document.createElement('p')
$style.textContent = `
.info { display: flex; }
.info-avatar { width: 100px; height: 100px; border-radius: 50%; }
.info-name { display: flex; align-items: center; font-size: 16px; }
`
$wrap.className = 'info'
$avatar.className = 'info-avatar'
$name.className = 'info-name'
$avatar.src = avatar
$name.innerHTML = name
$wrap.appendChild($avatar)
$wrap.appendChild($name)
$shadow.appendChild($style)
$shadow.appendChild($wrap)
}
customElements.define('info-card', InfoCard)
</script>
</body>
</html>