-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
461 lines (419 loc) · 13 KB
/
app.js
File metadata and controls
461 lines (419 loc) · 13 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* eslint-disable */
var customSearch;
(function ($) {
"use strict";
// 校正页面定位(被导航栏挡住的区域)
var scrollCorrection = 80; // (header height = 64px) + (gap = 16px)
var $headerAnchor = $('.l_header', '.cover-wrapper');
if ($headerAnchor[0]) {
scrollCorrection = $headerAnchor[0].clientHeight + 16;
}
// 尝试: 重设数据值
function restData() {
scrollCorrection = 80;
$headerAnchor = $('.l_header', '.cover-wrapper');
if ($headerAnchor[0]) {
scrollCorrection = $headerAnchor[0].clientHeight + 16;
}
}
// 校正页面定位(被导航栏挡住的区域)
function scrolltoElement(elem, correction = scrollCorrection) {
const $elem = elem.href ? $(decodeURI(elem.getAttribute('href'))) : $(elem);
$('html, body').animate({
'scrollTop': $elem.offset().top - correction
}, 500);
}
// 设置滚动锚点
function setScrollAnchor() {
const $postsBtn = $('.menu .active'); // 一级导航上的当前激活的按钮
const $topBtn = $('.s-top'); // 向上
const $titleBtn = $('h1.title', '#header-meta'); // 文章内标题
const $bodyAnchor = $('.l_body'); // 页面主体
if ($postsBtn.length && $bodyAnchor) {
$postsBtn.click(e => {
e.preventDefault();
e.stopPropagation();
if($postsBtn.attr("href") != "/") // TODO: fix it
scrolltoElement($bodyAnchor);
e.stopImmediatePropagation();
$postsBtn.unbind('click');
});
}
if ($titleBtn.length && $bodyAnchor) {
$titleBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
e.stopImmediatePropagation();
$titleBtn.unbind('click');
});
}
if ($topBtn.length && $bodyAnchor) {
$topBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
e.stopImmediatePropagation();
});
}
//==========================================
const $coverAnchor = $('.cover-wrapper');
var enableCover = $('#pjax-enable-cover').text(); // Pjax 处理
var showHeaderPoint = 0;
if ($coverAnchor[0]) {
if(enableCover == "true" && $('.cover.half').css('display') !== 'none') // Pjax 处理
showHeaderPoint = $coverAnchor[0].clientHeight - 180;
}
var pos = document.body.scrollTop;
if(enableCover == "true" && $('.cover.half').css('display') === 'none')
pos += 180; // Pjax 处理
$(document, window).scroll(() => {
let scrollTop = $(window).scrollTop(); // 滚动条距离顶部的距离
if(enableCover == "true" && $('.cover.half').css('display') === 'none')
scrollTop += 180; // Pjax 处理
const del = scrollTop - pos;
pos = scrollTop;
if (scrollTop > 180) {
$topBtn.addClass('show');
if (del > 0) {
$topBtn.removeClass('hl');
} else {
$topBtn.addClass('hl');
}
} else {
$topBtn.removeClass('show').removeClass('hl');
}
if (scrollTop > showHeaderPoint) {
$headerAnchor.addClass('show');
} else {
$headerAnchor.removeClass('show');
}
});
//==========================================
}
// 设置导航栏
function setHeader() {
var HEXO_ISPAGE = $.trim($('#pjax-ispage').text());
if(HEXO_ISPAGE == 'true')
window.subData = {
title: $.trim($('#pjax-pageTitle').text()),
tools: true
}
if (!window.subData) return;
const $wrapper = $('header .wrapper'); // 整个导航栏
const $comment = $('.s-comment', $wrapper); // 评论按钮 桌面端 移动端
const $toc = $('.s-toc', $wrapper); // 目录按钮 仅移动端
$wrapper.find('.nav-sub .title').text(window.subData.title); // 二级导航文章标题
// 决定一二级导航栏的切换
let pos = document.body.scrollTop;
$(document, window).scroll(() => {
const scrollTop = $(window).scrollTop();
const del = scrollTop - pos;
if (del >= 50 && scrollTop > 100) {
pos = scrollTop;
$wrapper.addClass('sub');
} else if (del <= -50) {
pos = scrollTop;
$wrapper.removeClass('sub'); // <---- 取消二级导航显示
}
});
// bind events to every btn
let $commentTarget = $('.l_body .comments'); // 评论区域
if ($commentTarget.length) {
$comment.click(e => { // 评论按钮点击后 跳转到评论区域
e.preventDefault();
e.stopPropagation();
scrolltoElement($('.l_body .comments'));
e.stopImmediatePropagation();
});
} else $comment.remove(); // 关闭了评论,则隐藏
const $tocTarget = $('.l_body .toc-wrapper'); // 侧边栏的目录列表 PC
if ($tocTarget.length && $tocTarget.children().length) {
$toc.click((e) => {
e.stopPropagation();
$tocTarget.toggleClass('active');
$toc.toggleClass('active');
});
$(document).click(function (e) {
e.stopPropagation();
$tocTarget.removeClass('active');
$toc.removeClass('active');
});
$(document, window).scroll(() => {
$tocTarget.removeClass('active');
$toc.removeClass('active');
});
} else $toc.remove();
}
// 设置导航栏菜单选中状态
function setHeaderMenuSelection() {
var $headerMenu = $('body .navigation');
// 先把已经激活的取消激活
$headerMenu.find('li a.active').removeClass('active');
$headerMenu.find('div a.active').removeClass('active');
// var $underline = $headerMenu.find('.underline');
function setUnderline($item) {
// if (!transition) $underline.addClass('disable-trans');
if ($item && $item.length) {
$item.addClass('active').siblings().removeClass('active');
}
}
//set current active nav
var $active_link = null;
// replace '%' '/' '.'
var idname = location.pathname.replace(/\/|%|\./g, "");
if (idname.length == 0) {
idname = "home";
}
var page = idname.match(/page\d{0,}$/g);
if (page) {
page = page[0];
idname = idname.split(page)[0];
}
var index = idname.match(/index.html/);
if (index) {
index = index[0];
idname = idname.split(index)[0];
}
if (idname && $headerMenu) {
$active_link = $('#' + idname, $headerMenu);
setUnderline($active_link);
}
}
// 设置全局事件
function setGlobalHeaderMenuEvent() {
// PC端 hover时展开子菜单,点击时隐藏子菜单
$('.m-pc li > a[href]').parent().click(function (e) {
e.stopPropagation();
$('.m-pc .list-v').hide();
});
// 手机端 点击展开子菜单
$('.m-phone li').click(function (e) {
e.stopPropagation();
$($(e.currentTarget).children('ul')).show();
});
setPageHeaderMenuEvent();
}
function setPageHeaderMenuEvent() {
// 手机端 点击空白处隐藏子菜单
$(document).click(function (e) {
$('.m-phone .list-v').hide();
});
// 手机端 滚动时隐藏子菜单
$(window).scroll(() => {
$('.m-phone .list-v').hide();
});
}
// 设置导航栏搜索框 fix √
function setHeaderSearch() {
var $switcher = $('.l_header .switcher .s-search'); // 搜索按钮 移动端
var $header = $('.l_header'); // 移动端导航栏
var $search = $('.l_header .m_search'); // 搜索框 桌面端
if ($switcher.length === 0) return;
$switcher.click(function (e) {
e.stopPropagation();
$header.toggleClass('z_search-open'); // 激活移动端搜索框
$switcher.toggleClass('active'); // 搜索按钮
$search.find('input').focus();
});
$(document).click(function (e) {
$header.removeClass('z_search-open');
$switcher.removeClass('active');
});
$search.click(function (e) {
e.stopPropagation();
});
$header.ready(function () {
$header.bind('keydown', function (event) {
if (event.keyCode == 9) {
return false;
} else {
var isie = (document.all) ? true : false;
var key;
var ev;
if (isie) { //IE浏览器
key = window.event.keyCode;
ev = window.event;
} else { //火狐浏览器
key = event.which;
ev = event;
}
if (key == 9) { //IE浏览器
if (isie) {
ev.keyCode = 0;
ev.returnValue = false;
} else { //火狐浏览器
ev.which = 0;
ev.preventDefault();
}
}
}
});
});
}
// 设置导航栏搜索框
function setTocToggle() {
const $toc = $('.toc-wrapper'); // 侧边栏 TOC 移动端
if ($toc.length === 0) return;
$toc.click((e) => {
e.stopPropagation();
$toc.addClass('active');
});
$(document).click(() => $toc.removeClass('active'));
$toc.on('click', 'a', (e) => {
e.preventDefault();
e.stopPropagation();
if (e.target.tagName === 'A') {
scrolltoElement(e.target, 0);
} else if (e.target.tagName === 'SPAN') {
scrolltoElement(e.target.parentElement, 0);
}
$toc.removeClass('active');
const $tocBtn = $('.s-toc');
if ($tocBtn.length > 0) {
$tocBtn.removeClass('active');
}
});
let liElements = Array.from($toc.find('li a'));
//function animate above will convert float to int.
let getAnchor = () => liElements.map(elem => Math.floor($(decodeURI(elem.getAttribute('href'))).offset().top - scrollCorrection));
let anchor = getAnchor();
let domHeigth = $(document).height();
let scrollListener = () => {
let scrollTop = $('html').scrollTop() || $('body').scrollTop();
if ($(document).height() != domHeigth) { // dom 高度发生变化: 普遍来说,是图片懒加载造成的
scrollTop = $('html').scrollTop() || $('body').scrollTop();
domHeigth = $(document).height();
anchor = getAnchor();
}
if (!anchor) return;
//binary search.
let l = 0,
r = anchor.length - 1,
mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (anchor[mid] === scrollTop) l = r = mid;
else if (anchor[mid] < scrollTop) l = mid;
else r = mid - 1;
}
$(liElements).removeClass('active').eq(l).addClass('active');
};
$(window).scroll(() => {
scrollListener();
});
// 监听窗口改变事件
let resizeTimer = null;
$(window).bind('resize', function (){
if (resizeTimer) clearTimeout(resizeTimer);
resizeTimer = setTimeout(function(){
anchor = getAnchor();
scrollListener();
} , 100);
});
scrollListener();
}
// 设置搜索服务
function setSearchService() {
if (SEARCH_SERVICE === 'google') {
customSearch = new GoogleCustomSearch({
apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY,
engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID,
imagePath: "/img/"
});
} else if (SEARCH_SERVICE === 'algolia') {
customSearch = new AlgoliaSearch({
apiKey: ALGOLIA_API_KEY,
appId: ALGOLIA_APP_ID,
indexName: ALGOLIA_INDEX_NAME,
imagePath: "/img/"
});
} else if (SEARCH_SERVICE === 'hexo') {
customSearch = new HexoSearch({
imagePath: "/img/"
});
} else if (SEARCH_SERVICE === 'azure') {
customSearch = new AzureSearch({
serviceName: AZURE_SERVICE_NAME,
indexName: AZURE_INDEX_NAME,
queryKey: AZURE_QUERY_KEY,
imagePath: "/img/"
});
} else if (SEARCH_SERVICE === 'baidu') {
customSearch = new BaiduSearch({
apiId: BAIDU_API_ID,
imagePath: "/img/"
});
}
}
// 设置 tabs 标签
function setTabs() {
const $tabs = $('.tabs');
if ($tabs.length === 0) return;
let $navs = $tabs.find('.nav-tabs .tab');
for (var i = 0; i < $navs.length; i++) {
let $a = $tabs.find($navs[i].children[0]);
$a.addClass($a.attr("href"));
$a.removeAttr('href');
}
$('.tabs .nav-tabs').on('click', 'a', (e) => {
e.preventDefault();
e.stopPropagation();
let $tab = $(e.target.parentElement.parentElement.parentElement);
$tab.find('.nav-tabs .active').removeClass('active');
$tab.find(e.target.parentElement).addClass('active');
$tab.find('.tab-content .active').removeClass('active');
$tab.find($(e.target).attr("class")).addClass('active');
return false;
});
}
$(function () {
setHeader();
setHeaderMenuSelection();
setGlobalHeaderMenuEvent();
setHeaderSearch();
setTocToggle();
setScrollAnchor();
setSearchService();
setTabs();
// 全屏封面底部箭头
$('.scroll-down').on('click', function () {
scrolltoElement('.l_body');
});
try {
// addEventListener是先绑定先执行,此处的绑定后执行
document.addEventListener('pjax:complete', function () {
$(function () {
restData();
setHeader();
setHeaderMenuSelection();
setPageHeaderMenuEvent();
setTocToggle();
setScrollAnchor();
setTabs();
// 处理点击事件 setHeaderSearch 没有重载,需要重新绑定单个事件
var $switcher = $('.l_header .switcher .s-search'); // 搜索按钮 移动端
var $header = $('.l_header'); // 移动端导航栏
if ($switcher.length !== 0) {
$(document).click(function (e) {
$header.removeClass('z_search-open');
$switcher.removeClass('active');
});
}
});
});
} catch (error) {
// console.log(error);
}
});
})(jQuery);
/*锚点定位*/
if(window.location.hash){
var checkExist = setInterval(function() {
if (typeof jQuery == 'undefined'){return;}
if ($("#"+decodeURI(window.location.hash.split("#")[1]).replace(/\ /g,"-")).length) {
$('html, body').animate({scrollTop: $("#"+decodeURI(window.location.hash.split("#")[1]).replace(/\ /g,"-")).offset().top-40}, 500);
clearInterval(checkExist);
}
}, 100);
}