@@ -21,81 +21,92 @@ $(function() {
2121 . not ( "[href='#']" )
2222 . not ( "[href='#0']" )
2323 . click ( function ( event ) {
24- if ( this . pathname . replace ( / ^ \/ / , "" ) === location . pathname . replace ( / ^ \/ / , "" ) ) {
25- if ( location . hostname === this . hostname ) {
26- const hash = decodeURI ( this . hash ) ;
27- let toFootnoteRef = RegExp ( / ^ # f n r e f : / ) . test ( hash ) ;
28- let toFootnote = toFootnoteRef ? false : RegExp ( / ^ # f n : / ) . test ( hash ) ;
29- let selector = hash . includes ( ":" ) ? hash . replace ( / \: / g, "\\:" ) : hash ;
30- let $target = $ ( selector ) ;
31-
32- let parent = $ ( this ) . parent ( ) . prop ( "tagName" ) ;
33- let isAnchor = RegExp ( / ^ H \d / ) . test ( parent ) ;
34- let isMobileViews = ! $topbarTitle . is ( ":hidden" ) ;
35-
36- if ( typeof $target !== "undefined" ) {
37- event . preventDefault ( ) ;
38-
39- if ( history . pushState ) { /* add hash to URL */
40- history . pushState ( null , null , hash ) ;
41- }
42-
43- let curOffset = isAnchor ? $ ( this ) . offset ( ) . top : $ ( window ) . scrollTop ( ) ;
44- let destOffset = $target . offset ( ) . top -= REM / 2 ;
45-
46- if ( destOffset < curOffset ) { // scroll up
47- if ( ! isAnchor && ! toFootnote ) { // trigger by ToC item
48- if ( ! isMobileViews ) { // on desktop/tablet screens
49- $topbarWrapper . removeClass ( "topbar-down" ) . addClass ( "topbar-up" ) ;
50- // Send message to `${JS_ROOT}/commons/topbar-switch.js`
51- $topbarWrapper . attr ( ATTR_TOC_SCROLLING , true ) ;
52- tocScrollUpCount += 1 ;
53- }
54- }
55-
56- if ( ( isAnchor || toFootnoteRef ) && isMobileViews ) {
57- destOffset -= topbarHeight ;
58- }
59- }
60-
61- $ ( "html" ) . animate ( {
62- scrollTop : destOffset
63- } , 500 , ( ) => {
64- $target . focus ( ) ;
65-
66- /* clean up old scroll mark */
67- if ( $ ( `[${ SCROLL_MARK } =true]` ) . length ) {
68- $ ( `[${ SCROLL_MARK } =true]` ) . attr ( SCROLL_MARK , false ) ;
69- }
70-
71- /* Clean :target links */
72- if ( $ ( ":target" ) . length ) { /* element that visited by the URL with hash */
73- $ ( ":target" ) . attr ( SCROLL_MARK , false ) ;
74- }
75-
76- /* set scroll mark to footnotes */
77- if ( toFootnote || toFootnoteRef ) {
78- $target . attr ( SCROLL_MARK , true ) ;
79- }
80-
81- if ( $target . is ( ":focus" ) ) { /* Checking if the target was focused */
82- return false ;
83- } else {
84- $target . attr ( "tabindex" , "-1" ) ; /* Adding tabindex for elements not focusable */
85- $target . focus ( ) ; /* Set focus again */
86- }
87-
88- if ( typeof $topbarWrapper . attr ( ATTR_TOC_SCROLLING ) !== "undefined" ) {
89- tocScrollUpCount -= 1 ;
90-
91- if ( tocScrollUpCount <= 0 ) {
92- $topbarWrapper . attr ( ATTR_TOC_SCROLLING , "false" ) ;
93- }
94- }
95- } ) ;
24+
25+ if ( this . pathname . replace ( / ^ \/ / , "" ) !== location . pathname . replace ( / ^ \/ / , "" ) ) {
26+ return ;
27+ }
28+
29+ if ( location . hostname !== this . hostname ) {
30+ return ;
31+ }
32+
33+ const hash = decodeURI ( this . hash ) ;
34+ let toFootnoteRef = RegExp ( / ^ # f n r e f : / ) . test ( hash ) ;
35+ let toFootnote = toFootnoteRef ? false : RegExp ( / ^ # f n : / ) . test ( hash ) ;
36+ let selector = hash . includes ( ":" ) ? hash . replace ( / \: / g, "\\:" ) : hash ;
37+ let $target = $ ( selector ) ;
38+
39+ let parent = $ ( this ) . parent ( ) . prop ( "tagName" ) ;
40+ let isAnchor = RegExp ( / ^ H \d / ) . test ( parent ) ;
41+ let isMobileViews = ! $topbarTitle . is ( ":hidden" ) ;
42+
43+ if ( typeof $target === "undefined" ) {
44+ return ;
45+ }
46+
47+ event . preventDefault ( ) ;
48+
49+ if ( history . pushState ) { /* add hash to URL */
50+ history . pushState ( null , null , hash ) ;
51+ }
52+
53+ let curOffset = isAnchor ? $ ( this ) . offset ( ) . top : $ ( window ) . scrollTop ( ) ;
54+ let destOffset = $target . offset ( ) . top -= REM / 2 ;
55+
56+ if ( destOffset < curOffset ) { // scroll up
57+ if ( ! isAnchor && ! toFootnote ) { // trigger by ToC item
58+ if ( ! isMobileViews ) { // on desktop/tablet screens
59+ $topbarWrapper . removeClass ( "topbar-down" ) . addClass ( "topbar-up" ) ;
60+ // Send message to `${JS_ROOT}/commons/topbar-switch.js`
61+ $topbarWrapper . attr ( ATTR_TOC_SCROLLING , true ) ;
62+ tocScrollUpCount += 1 ;
9663 }
9764 }
65+
66+ if ( ( isAnchor || toFootnoteRef ) && isMobileViews ) {
67+ destOffset -= topbarHeight ;
68+ }
69+
70+ } else {
71+ if ( isMobileViews ) {
72+ destOffset -= topbarHeight ;
73+ }
9874 }
9975
76+ $ ( "html" ) . animate ( {
77+ scrollTop : destOffset
78+ } , 500 , ( ) => {
79+ $target . focus ( ) ;
80+
81+ /* clean up old scroll mark */
82+ if ( $ ( `[${ SCROLL_MARK } =true]` ) . length ) {
83+ $ ( `[${ SCROLL_MARK } =true]` ) . attr ( SCROLL_MARK , false ) ;
84+ }
85+
86+ /* Clean :target links */
87+ if ( $ ( ":target" ) . length ) { /* element that visited by the URL with hash */
88+ $ ( ":target" ) . attr ( SCROLL_MARK , false ) ;
89+ }
90+
91+ /* set scroll mark to footnotes */
92+ if ( toFootnote || toFootnoteRef ) {
93+ $target . attr ( SCROLL_MARK , true ) ;
94+ }
95+
96+ if ( $target . is ( ":focus" ) ) { /* Checking if the target was focused */
97+ return false ;
98+ } else {
99+ $target . attr ( "tabindex" , "-1" ) ; /* Adding tabindex for elements not focusable */
100+ $target . focus ( ) ; /* Set focus again */
101+ }
102+
103+ if ( typeof $topbarWrapper . attr ( ATTR_TOC_SCROLLING ) !== "undefined" ) {
104+ tocScrollUpCount -= 1 ;
105+
106+ if ( tocScrollUpCount <= 0 ) {
107+ $topbarWrapper . attr ( ATTR_TOC_SCROLLING , "false" ) ;
108+ }
109+ }
110+ } ) ;
100111 } ) ; /* click() */
101112} ) ;
0 commit comments