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' )
5
3
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 ( '>>' )
9
16
10
- let apiContainerStyle = `
17
+ let status = true // true:open; false:close
18
+ let apiContainerWidth = 0
19
+ let handShankWidth = 32
20
+
21
+ let apiContainerStyle = `
11
22
position:fixed;
12
23
z-index:10;
13
24
right:-18px;
@@ -17,12 +28,12 @@ let apiContainerStyle = `
17
28
padding-bottom:60px;
18
29
`
19
30
20
- let apiTreeStyle = `
31
+ let apiTreeStyle = `
21
32
height:100%;
22
33
overflow-y:scroll;
23
34
`
24
35
25
- let handShankStyle = `
36
+ let handShankStyle = `
26
37
position:absolute;
27
38
top:50%;
28
39
left:0;
@@ -37,22 +48,29 @@ let handShankStyle = `
37
48
border-radius:0 10px 10px 0
38
49
`
39
50
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
43
54
44
- handShank . appendChild ( arrowRight )
45
- apiContainer . appendChild ( handShank )
55
+ handShank . appendChild ( arrowRight )
56
+ apiContainer . appendChild ( handShank )
46
57
47
- // after position:fixed get the real width
48
- apiContainerWidth = apiContainer . clientWidth
58
+ // after position:fixed get the real width
59
+ apiContainerWidth = apiContainer . clientWidth
49
60
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