Skip to content

Commit f4eb67b

Browse files
committed
fix dom bug
1 parent 538edd6 commit f4eb67b

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

js/better-view.js

+44-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
const apiContainer = document.querySelector('#toc')
2-
const apiTree = apiContainer.querySelector('ul')
3-
const handShank = document.createElement('div')
4-
const arrowRight = document.createTextNode('>>')
1+
(()=>{
2+
const apiContainer = document.querySelector('#toc')
53

6-
let status = true // true:open; false:close
7-
let apiContainerWidth = 0
8-
let handShankWidth = 32
4+
if (!isInPage(apiContainer)){
5+
return;
6+
}
7+
8+
const apiTree = apiContainer.querySelector('ul')
9+
10+
if(!isInPage(apiTree)){
11+
return;
12+
}
13+
14+
const handShank = document.createElement('div')
15+
const arrowRight = document.createTextNode('>>')
916

10-
let apiContainerStyle = `
17+
let status = true // true:open; false:close
18+
let apiContainerWidth = 0
19+
let handShankWidth = 32
20+
21+
let apiContainerStyle = `
1122
position:fixed;
1223
z-index:10;
1324
right:-18px;
@@ -17,12 +28,12 @@ let apiContainerStyle = `
1728
padding-bottom:60px;
1829
`
1930

20-
let apiTreeStyle = `
31+
let apiTreeStyle = `
2132
height:100%;
2233
overflow-y:scroll;
2334
`
2435

25-
let handShankStyle = `
36+
let handShankStyle = `
2637
position:absolute;
2738
top:50%;
2839
left:0;
@@ -37,22 +48,29 @@ let handShankStyle = `
3748
border-radius:0 10px 10px 0
3849
`
3950

40-
handShank.style.cssText = handShankStyle
41-
apiContainer.style.cssText = apiContainerStyle
42-
apiTree.style.cssText = apiTreeStyle
51+
handShank.style.cssText = handShankStyle
52+
apiContainer.style.cssText = apiContainerStyle
53+
apiTree.style.cssText = apiTreeStyle
4354

44-
handShank.appendChild(arrowRight)
45-
apiContainer.appendChild(handShank)
55+
handShank.appendChild(arrowRight)
56+
apiContainer.appendChild(handShank)
4657

47-
// after position:fixed get the real width
48-
apiContainerWidth = apiContainer.clientWidth
58+
// after position:fixed get the real width
59+
apiContainerWidth = apiContainer.clientWidth
4960

50-
handShank.addEventListener('click', function (params) {
51-
if (status) {
52-
apiContainer.style.cssText = `${apiContainerStyle};right:-${apiContainerWidth - handShankWidth}px`
53-
status = false
54-
} else {
55-
apiContainer.style.cssText = apiContainerStyle
56-
status = true
57-
}
58-
}, true)
61+
handShank.addEventListener('click', function (params) {
62+
if (status) {
63+
apiContainer.style.cssText = `${apiContainerStyle};right:-${apiContainerWidth - handShankWidth}px`
64+
status = false
65+
} else {
66+
apiContainer.style.cssText = apiContainerStyle
67+
status = true
68+
}
69+
}, true)
70+
71+
})()
72+
73+
// check node is exist
74+
function isInPage(node){
75+
return document.body.contains(node)
76+
}

0 commit comments

Comments
 (0)