64
64
var back_to_top = '<a class="jekyll-toc-anchor jekyll-toc-back-to-top"><span class="jekyll-toc-icon">' + settings . backToTopText + '</span></a>' ;
65
65
var link_here = '<a class="jekyll-toc-anchor jekyll-toc-link-here"><span class="jekyll-toc-icon">' + settings . linkHereText + '</span></a>' ;
66
66
67
+ function force_update_hash ( hash ) {
68
+ window . location . hash = '' ;
69
+ window . location . hash = hash ;
70
+ }
71
+
72
+ function update_hash ( hash ) {
73
+ if ( history . pushState ) {
74
+ history . pushState ( null , null , '#' + hash ) ;
75
+ } else {
76
+ force_update_hash ( hash )
77
+ }
78
+ }
79
+
67
80
function animate_link_here ( header_id ) {
81
+ update_hash ( ( typeof header_id === "undefined" ) ? '' : header_id ) ;
68
82
$ ( 'html,body' ) . animate ( { scrollTop :$ ( document . getElementById ( header_id ) ) . offset ( ) . top } , settings . linkHereDuration ) ;
69
- window . location . hash = ( header_id === "undefined" ) ? '' : header_id ;
70
83
}
71
-
84
+
72
85
if ( settings . backToTop ) {
73
86
$ ( document ) . on ( 'click' , '.jekyll-toc-back-to-top' , function ( ) {
74
- if ( settings . backToTopSelector == '' ) {
75
- $ ( 'html, body' ) . animate ( { scrollTop : $ ( 'html, body' ) . offset ( ) . top } , settings . backToTopDuration ) ;
76
- window . location . hash = '' ;
87
+ if ( settings . backToTopSelector == '' ) {
88
+ if ( settings . backToTopDuration > 0 ) {
89
+ update_hash ( '' ) ;
90
+ $ ( 'html, body' ) . animate ( { scrollTop : $ ( 'html, body' ) . offset ( ) . top } , settings . backToTopDuration ) ;
91
+ } else {
92
+ force_update_hash ( '' ) ;
93
+ }
77
94
} else {
78
- var top_element = $ ( settings . backToTopSelector ) . first ( ) ;
79
- $ ( 'html, body' ) . animate ( { scrollTop : top_element . offset ( ) . top } , settings . backToTopDuration ) ;
95
+ var top_element = ( $ ( settings . backToTopSelector ) . length ) ? $ ( settings . backToTopSelector ) . first ( ) : $ ( 'html, body' ) ;
80
96
var top_element_id = $ ( top_element ) . attr ( 'id' ) ;
81
- window . location . hash = ( typeof top_element_id === "undefined" ) ? '' : top_element_id ;
97
+ update_hash ( ( typeof top_element_id === "undefined" ) ? '' : top_element_id ) ;
98
+ if ( settings . backToTopDuration > 0 || window . location . hash == '' ) {
99
+ $ ( 'html, body' ) . animate ( { scrollTop : top_element . offset ( ) . top } , settings . backToTopDuration ) ;
100
+ } else {
101
+ force_update_hash ( window . location . hash ) ;
102
+ }
82
103
}
83
104
} ) ;
84
105
}
132
153
133
154
render [ settings . showEffect ] ( ) ;
134
155
} ;
135
- } ) ( jQuery ) ;
156
+ } ) ( jQuery ) ;
0 commit comments