-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
scroll.js
179 lines (162 loc) · 5.05 KB
/
scroll.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
$(function () {
var initTop = 0
$('.toc-child').hide()
// main of scroll
$(window).scroll(throttle(function (event) {
var currentTop = $(this).scrollTop()
if (!isMobile()) {
// percentage inspired by hexo-theme-next
scrollPercent(currentTop)
// head position
findHeadPosition(currentTop)
}
var isUp = scrollDirection(currentTop)
if (currentTop > 56) {
if (isUp) {
$('#page-header').hasClass('visible') ? $('#page-header').removeClass('visible') : console.log()
} else {
$('#page-header').hasClass('visible') ? console.log() : $('#page-header').addClass('visible')
}
$('#page-header').addClass('fixed')
if ($('#go-up').css('opacity') === '0') {
$('#go-up').velocity('stop').velocity({
translateX: -30,
rotateZ: 360,
opacity: 1
}, {
easing: 'easeOutQuart',
duration: 200
})
}
} else {
if (currentTop === 0) {
$('#page-header').removeClass('fixed').removeClass('visible')
}
$('#go-up').velocity('stop').velocity({
translateX: 0,
rotateZ: 180,
opacity: 0
}, {
easing: 'linear',
duration: 200
})
}
}, 50, 100))
// go up smooth scroll
$('#go-up').on('click', function () {
$('body').velocity('stop').velocity('scroll', {
duration: 500,
easing: 'easeOutQuart'
})
})
// head scroll
$('#post-content').find('h1,h2,h3,h4,h5,h6').on('click', function (e) {
scrollToHead('#' + $(this).attr('id'))
})
// head scroll
$('.toc-link').on('click', function (e) {
e.preventDefault()
scrollToHead($(this).attr('href'))
})
// find the scroll direction
function scrollDirection (currentTop) {
var result = currentTop > initTop // true is down & false is up
initTop = currentTop
return result
}
// scroll to a head(anchor)
function scrollToHead (anchor) {
var item
try {
item = $(anchor)
} catch (e) {
// fix #286 support hexo v5
item = $(decodeURI(anchor))
}
item.velocity('stop').velocity('scroll', {
duration: 500,
easing: 'easeInOutQuart'
})
}
// expand toc-item
function expandToc ($item) {
if ($item.is(':visible')) {
return
}
$item.velocity('stop').velocity('transition.fadeIn', {
duration: 500,
easing: 'easeInQuart'
})
}
function scrollPercent (currentTop) {
var docHeight = $('#content-outer').height()
var winHeight = $(window).height()
var contentMath = (docHeight > winHeight) ? (docHeight - winHeight) : ($(document).height() - winHeight)
var scrollPercent = (currentTop) / (contentMath)
var scrollPercentRounded = Math.round(scrollPercent * 100)
var percentage = (scrollPercentRounded > 100) ? 100
: (scrollPercentRounded <= 0) ? 0
: scrollPercentRounded
$('.progress-num').text(percentage)
$('.sidebar-toc__progress-bar').velocity('stop')
.velocity({
width: percentage + '%'
}, {
duration: 100,
easing: 'easeInOutQuart'
})
}
function updateAnchor (anchor) {
if (window.history.replaceState && anchor !== window.location.hash) {
window.history.replaceState(undefined, undefined, anchor)
}
}
// find head position & add active class
// DOM Hierarchy:
// ol.toc > (li.toc-item, ...)
// li.toc-item > (a.toc-link, ol.toc-child > (li.toc-item, ...))
function findHeadPosition (top) {
// assume that we are not in the post page if no TOC link be found,
// thus no need to update the status
if ($('.toc-link').length === 0) {
return false
}
var list = $('#post-content').find('h1,h2,h3,h4,h5,h6')
var currentId = ''
list.each(function () {
var head = $(this)
if (top > head.offset().top - 25) {
currentId = '#' + $(this).attr('id')
}
})
if (currentId === '') {
$('.toc-link').removeClass('active')
$('.toc-child').hide()
}
// fix #286 since hexo v5.0.0 will
// encodeURI the toc-item href
var hexoVersion = GLOBAL_CONFIG.hexoVersion[0]
if (parseInt(hexoVersion) >= 5) {
currentId = encodeURI(currentId)
}
var currentActive = $('.toc-link.active')
if (currentId && currentActive.attr('href') !== currentId) {
updateAnchor(currentId)
$('.toc-link').removeClass('active')
var _this = $('.toc-link[href="' + currentId + '"]')
_this.addClass('active')
var parents = _this.parents('.toc-child')
// Returned list is in reverse order of the DOM elements
// Thus `parents.last()` is the outermost .toc-child container
// i.e. list of subsections
var topLink = (parents.length > 0) ? parents.last() : _this
expandToc(topLink.closest('.toc-item').find('.toc-child'))
topLink
// Find all top-level .toc-item containers, i.e. sections
// excluding the currently active one
.closest('.toc-item').siblings('.toc-item')
// Hide their respective list of subsections
.find('.toc-child').hide()
}
}
})