-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.js
1093 lines (991 loc) · 35.5 KB
/
app.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* HomePage: https://www.misterma.com
* GithubPage: https://github.com/changbin1997
* ProjectPage: https://github.com/changbin1997/Facile
* author: Mr. Ma
* Licensed under MIT
*/
$(function () {
let maxImg = false; // 是否开启图片灯箱
let directory = false; // 是否打开移动设备的目录
let emojiList = null; // Emoji 列表
let showEmoji = false; // Emoji 面板状态
const avatarColor = []; // 存储文字头像颜色
const avatarName = []; // 存储文字头像名称
let directoryTop = 0; // 侧边栏章节目录的高度
let commentParentId = null; // 存储父评论的id,用于PJAX评论提交后跳转
// 主题配色初始化
themeColorInit();
// 给分页链接添加class和aria属性
paginationLinkInit();
// 头像样式初始化
avatarStyleInit();
// 给文章内的表格添加 bootstrap 的样式
tableInit();
// 给文章中的代码块添加拷贝按钮和拷贝事件
codeHighlightInit();
// 图片灯箱初始化
imageLightboxInit();
// 点赞初始化
likeInit();
// Emoji 面板初始化
emojiInit();
// bootstrap 的一些样式初始化
bootstrapStyleInit();
// 文章内的章节目录跳转样式
directoryStyleInit();
// 一些可访问性相关的功能初始化
accessibilityInit();
// 生成文章的分享二维码
shareQrCode();
// 图片懒加载
lazyLoadImages();
// 全局快捷键
$(document).on('keyup', ev => {
// 如果是 ESC 就关闭大图
if (ev.keyCode === 27 && $('#max-img-box').length) {
$('.max-img-features-btn .hide-img').click();
}
// 如果按下的是 + 就放大图片
if (ev.keyCode === 107 && $('#max-img-box').length) {
$('.max-img-features-btn .big').click();
}
// 如果按下的是 - 就缩小图片
if (ev.keyCode === 109 && $('#max-img-box').length) {
$('.max-img-features-btn .small').click();
}
// 如果按下的是右方向键就跳转到下一页
if (ev.keyCode === 39) {
// 文章列表页面跳转
if ($('.next .page-link').length) {
location.href = $('.next .page-link').attr('href');
}
// 文章页内容翻页
if ($('.post-pagination .next-page').length) {
location.href = $('.post-pagination .next-page').attr('href');
}
}
// 如果按下的是左方向键就跳转到上一页
if (ev.keyCode === 37) {
// 文章列表页面跳转
if ($('.prev .page-link').length) {
location.href = $('.prev .page-link').attr('href');
}
// 文章页内容翻页
if ($('.post-pagination .previous-page').length) {
location.href = $('.post-pagination .previous-page').attr('href');
}
}
});
// 页面空白区域点击
$('body').on('click', () => {
// 如果表情面板处于开启状态就关闭表情面板
if (showEmoji) $('#show-emoji-btn').click();
});
// 评论内容输入框点击
$('#textarea').on('click', () => {
return false;
});
// 页面加载完成后调整侧边栏目录的高度
directorySize()
// 窗口尺寸改变
window.addEventListener('resize', () => {
// 调整侧边栏目录的高度
directorySize();
});
// 监听滚动条
$(document).on('scroll', () => {
// 返回顶部的按钮是否存在
if ($('#to-top-btn').length) {
// 如果滚动条高度 > 屏幕高度
if ($(document).scrollTop() > window.innerHeight) {
// 显示返回顶部按钮
$('#to-top-btn').removeClass('d-none');
}else {
// 隐藏返回顶部按钮
$('#to-top-btn').addClass('d-none');
}
}
// 检测文章图片位置
$('.load-img').each(function() {
// 如果文章内的 img 进入可视区就加载图片
if (
$(this).offset().top < $(document).scrollTop() + window.innerHeight &&
$(this).offset().top + $(this).height() > $(document).scrollTop()
) {
if ($(this).attr('src') === undefined) {
$(this).attr('src', $(this).attr('data-src'));
}
}
});
// 固定侧边栏章节目录位置
if ($('.sidebar .directory').length && window.innerWidth >= 992) {
if ($(document).scrollTop() >= directoryTop) {
$('.sidebar .directory').css({
position: 'fixed',
top: 80
});
}else {
$('.sidebar .directory').css('position', 'static');
}
}
});
// 返回顶部按钮点击
$('#to-top-btn').on('click', () => {
// 返回顶部,让第一个链接获取焦点
$('html').animate({
scrollTop: 0
}, 400);
$('header .navbar-brand').get(0).focus();
return false;
});
// PJAX 链接初始化
if ($('body').attr('data-pjax') === 'on') {
pjaxLinkInit();
}
// 初始化 PJAX
if ($('body').attr('data-pjax') === 'on') {
$(document).pjax('.pjax-link', '#main', {
fragment: '#main',
timeout: 20000
});
}
// PJAX 搜索表单提交
if ($('body').attr('data-pjax') === 'on') {
$(document).on('submit', 'form[role="search"]', ev => {
$.pjax.submit(ev, '#main', {
fragment: '#main',
replace: false,
timeout: 20000
});
});
}
// PJAX 评论表单提交
if ($('body').attr('data-pjax') === 'on') {
$(document).on('submit', '#comment-form', ev => {
// 如果是回复评论就存储父评论的id
if ($('#comment-parent').length && $('#comment-parent').val() !== '') {
commentParentId = $('#comment-parent').val();
}
$.pjax.submit(ev, '#main', {
fragment: '#main',
replace: false,
push: false,
timeout: 20000
});
});
}
// PJAX 即将开始请求
if ($('body').attr('data-pjax') === 'on') {
$(document).on('pjax:start', () => {
// 如果开启了移动设备的导航菜单就关闭菜单
if ($('.navbar-toggler').attr('aria-expanded') === 'true') {
$('.navbar-toggler').click();
}
// 移除工具提示
$('[data-toggle="tooltip"]').tooltip('dispose');
// 显示进度条
if ($('#progress-bar').length) {
$('#progress-bar').show();
}
});
}
// PJAX 开始请求
if ($('body').attr('data-pjax') === 'on') {
$(document).on('pjax:send', () => {
if ($('#progress-bar').length) {
// 更改进度条
$('#progress-bar #progress').animate({
width: '30%'
}, 100);
$('#progress-bar #progress').attr('aria-valuenow', '30');
}
});
}
// PJAX 请求完成
if ($('body').attr('data-pjax') === 'on') {
$(document).on('pjax:complete', () => {
if ($('#progress-bar').length) {
// 更改进度条
$('#progress-bar #progress').animate({
width: '80%'
}, 200);
$('#progress-bar #progress').attr('aria-valuenow', '80');
}
});
}
// PJAX 替换完成
if ($('body').attr('data-pjax') === 'on') {
$(document).on('pjax:end', (ev) => {
// 隐藏进度条
if ($('#progress-bar').length) {
$('#progress-bar #progress').animate({
width: '100%'
}, 100, () => {
$('#progress-bar').hide();
$('#progress-bar #progress').css('width', '0');
$('#progress-bar #progress').attr('aria-valuenow', '0');
});
$('#progress-bar #progress').attr('aria-valuenow', '100');
}
// 清除导航栏链接的选中状态
$('.navbar-nav .nav-item').removeClass('active');
$('.navbar-nav .nav-item a').removeAttr('aria-current');
// 重新设置导航栏链接的选中状态
for (let i = 0;i < $('.navbar-nav .nav-item a').length;i ++) {
if ($('.navbar-nav .nav-item a').eq(i).attr('href') === ev.currentTarget.URL) {
$('.navbar-nav .nav-item').eq(i).addClass('active');
$('.navbar-nav .nav-item a').eq(i).attr('aria-current', 'page');
break;
}
}
// 如果是评论提交就滚动到评论区
if (ev.relatedTarget.id === 'comment-form') {
if (commentParentId !== null && $(`#comment-${commentParentId}`).length) {
// 如果是回复评论就滚动到父评论的区域
$('html, body').animate({
scrollTop: $(`#comment-${commentParentId}`).offset().top
}, 250);
}else {
// 如果是评论提交就滚动到评论区
$('html, body').animate({
scrollTop: $('#comments').offset().top
}, 250);
}
commentParentId = null;
}
// 表格初始化
tableInit();
// 代码高亮初始化
codeHighlightInit();
// 图片灯箱初始化
imageLightboxInit();
// 分页链接初始化
paginationLinkInit();
// 调整章节目录的尺寸
directorySize();
// 文章章节目录跳转样式
directoryStyleInit();
// 头像样式初始化
avatarStyleInit();
// 点赞初始化
likeInit();
// Emoji 面板初始化
emojiInit();
// 初始化 bootstrap 的一些样式
bootstrapStyleInit();
// 主题配色初始化
themeColorInit();
// 一些可访问性相关的功能初始化
accessibilityInit();
// 给 PJAX 链接添加 class
pjaxLinkInit();
// 生成文章的分享二维码
shareQrCode();
// 图片懒加载
lazyLoadImages();
});
}
// 下面是一些用于样式和功能初始化的函数
// 图片懒加载
function lazyLoadImages() {
// 如果页面加载完成时有图片在可视区就直接加载图片
$('.load-img').each(function() {
if ($(this).offset().top < window.innerHeight) {
$(this).attr('src', $(this).attr('data-src'));
}
});
// 文章图片加载完成后删除默认样式
$('.load-img').on('load', function() {
$(this).removeClass('load-img');
});
}
// 给 PJAX 链接添加 class
function pjaxLinkInit() {
const currentDomain = window.location.hostname;
$('a').each((index, element) => {
const href = $(element).attr('href');
const target = $(element).attr('target');
// 检查链接是否包含当前域名,且不含有 target="_blank"
if (href && href.includes(currentDomain) && !target) {
$(element).addClass('pjax-link');
}
});
}
// 生成分享二维码
function shareQrCode() {
if ($('#qr') !== undefined) {
const qr = new QRious({
element: $('#qr').get(0),
value: location.href,
size: 150
});
}
}
// 一些可访问性相关的功能初始化
function accessibilityInit() {
// 文章是否加密
if ($('.post-content .protected').length) {
$('input[name="protectPassword"]').attr('placeholder', '请在此处输入文章密码');
$('input[name="protectPassword"]').focus();
}
// 给文章内的链接添加 target 属性
if ($('.post-page .post-content a').length) {
$('.post-content a').attr('target', '_blank');
}
// 给评论区的评论者链接添加 target 属性
if ($('.comment-info .author a').length) {
$('.comment-info .author a').attr('target', '_blank');
}
// 评论列表的回复链接点击
$('.comment-reply').on('click', () => {
if ($('.comment-list .comment-input').length && $('#cancel-comment-reply-link').length) {
$('#cancel-comment-reply-link').addClass('btn btn-outline-primary ml-2');
$('#cancel-comment-reply-link').attr('role', 'button');
}
});
// 取消回复链接按下 tab
$('#cancel-comment-reply-link').on('keydown', ev => {
if (ev.keyCode === 9) {
// 让评论内容输入框获取焦点
ev.preventDefault();
$('#textarea').focus();
}
});
// 给评论列表添加描述
if ($('.comments-lists > ol').length) {
$('.comments-lists > ol').attr('aria-label', '评论区');
}
// 回复对象名字鼠标移入和移出
$('#comments .parent').hover(ev => {
$($(ev.target).attr('href') + ' .comment-content').css({
background: '#D0210E',
color: '#FFFFFF'
});
}, ev => {
$($(ev.target).attr('href') + ' .comment-content').css({
background: 'none',
color: '#212529'
});
});
// 评论回复链接获取焦点
$('#comments .comment-reply a').on('focus', ev => {
const cid = $(ev.target).parent().attr('data-id');
$('#' + cid + ' .comment-content').css({
background: '#D0210E',
color: '#FFFFFF'
});
});
// 评论回复链接失去焦点
$('#comments .comment-reply a').on('blur', ev => {
const cid = $(ev.target).parent().attr('data-id');
$('#' + cid + ' .comment-content').css({
background: 'none',
color: '#212529'
});
});
}
// 主题配色初始化
function themeColorInit() {
// 根据当前使用的主题配色来设置侧边栏主题单选框的选中状态
if ($('.change-color').length) {
// 浅色模式
if ($('.light-color').length) $('#light-color').attr('checked', true);
// 深色模式
if ($('.dark-color').length) $('#dark-color').attr('checked', true);
// 如果使用了跟随系统主题就检测系统主题的配色模式
if ($('.auto-color').length) {
const darkColor = window.matchMedia('(prefers-color-scheme: dark)');
if (darkColor.matches) {
// 深色
$('#dark-color').attr('checked', true);
}else {
// 浅色
$('#light-color').attr('checked', true);
}
}
}
// 切换主题的单选框改变
$('.change-theme-color').on('click', ev => {
// 获取选中的颜色
const color = $(ev.target).attr('id');
// 获取当前的时间戳
let time = Date.parse(new Date());
// 在当前的时间戳上 + 180天
time += 15552000000;
time = new Date(time);
// 写入 cookie
document.cookie = 'themeColor=' + color + ';path=/;expires=Tue,' + time;
// 更改配色
$('body').removeClass($('body').attr('data-color'));
$('body').addClass(color);
$('body').attr('data-color', color);
});
}
// 一些 bootstrap 的样式初始化
function bootstrapStyleInit() {
// 初始化气球提示
$('[data-toggle="tooltip"]').tooltip();
// 给文章中的标签添加Bootstrap的样式
if ($('.post-tag a').length) {
$('.post-tag a').addClass('badge badge-dark');
}
}
// Emoji 面板初始化
function emojiInit() {
if ($('#emoji-panel').length) {
// 如果开启了 Emoji 面板就加载 Emoji
$.ajax({
type: 'post',
url: $('#show-emoji-btn').attr('data-url'),
data: 'emoji=emoji',
timeout: 10000,
global: false,
success: data => {
data = JSON.parse(data);
// 检查是否加载正确
if (data.smileys === undefined) {
$('#emoji-panel').append('<div>未知错误!</div>');
return false;
}
emojiList = data;
// 调用面目表情按钮事件
$('#emoji-classification button').eq(0).click();
},
error: (xhr, err, abnormal) => {
$('#emoji-panel').append('<div>服务器请求出错!错误代码' + err + '</div>');
}
});
// Emoji 开关点击
$('#show-emoji-btn').on('click', ev => {
// 设置 Emoji 面板的显示和隐藏
$('#emoji-panel').slideToggle(250);
// 设置 Emoji 的显示和隐藏状态
showEmoji = !showEmoji;
// 设置用于屏幕阅读器的 Emoji 面板的显示和隐藏状态
$(ev.target).attr('aria-expanded', showEmoji);
// 聚焦到 emoji 面板的第一个按钮
$('#emoji-panel button').eq(0).focus();
// 避免触发页面空白区域
return false;
});
// 切换Emoji类型按钮点击
$('#emoji-classification button').on('click', ev => {
const emoji = emojiList[$(ev.target).attr('data-classification')];
let emojiEl = '';
// 清除之前选中的按钮的选中状态
$('#emoji-classification .selected').attr('aria-checked', false);
$('#emoji-classification .selected').removeClass('selected');
// 设置点击按钮的选中状态
$(ev.target).attr('aria-checked', true);
$(ev.target).addClass('selected');
// 生成 Emoji 元素
emoji.forEach(e => {
emojiEl += '<div class="emoji p-2" tabindex="0" role="listitem">' + e + '</div>';
});
// 清除之前的 Emoji
if ($('#emoji-list .emoji').length) {
$('#emoji-list .emoji').remove();
}
// 把 Emoji 插入到页面
$('#emoji-list').append(emojiEl);
// 设置类型标题
$('#emoji-title').html($(ev.target).attr('title'));
// 设置用于屏幕阅读器的表情列表标题
$('#emoji-list').attr('aria-label', $(ev.target).attr('title') + '(按回车可以把表情添加到评论内容输入框)');
});
// Emoji 表情点击
$('#emoji-list').on('click', '.emoji', ev => {
// 把表情添加到评论内容输入框
$('#textarea').val($('#textarea').val() + $(ev.target).html());
});
// Emoji 表情按下回车或 Tab
$('#emoji-list').on('keydown', '.emoji', ev => {
// 按下回车键
if (ev.keyCode === 13) {
// 把表情添加到评论内容输入框
$('#textarea').val($('#textarea').val() + $(ev.target).html());
}
// 按下 Tab
if (ev.keyCode === 9 && $(ev.target).is('#emoji-list .emoji:last-child')) {
ev.preventDefault();
// 聚焦到 emoji 面板的第一个按钮
$('#emoji-panel button').eq(0).focus();
}
});
// Emoji 表情面板按下 ESC
$('#emoji-panel').on('keydown', ev => {
if (ev.keyCode === 27) {
// 调用 Emoji 开关事件
$('#show-emoji-btn').click();
$('#textarea').focus();
}
});
// Emoji 面板的空白区域点击
$('#emoji-panel').on('click', () => {
return false;
});
}
}
// 点赞初始化
function likeInit() {
if ($('.agree-btn').length) {
$('.agree-btn').on('click', () => {
$('.agree-btn').get(0).disabled = true;
$.ajax({
type: 'post',
url: $('.agree-btn').attr('data-url'),
data: 'agree=' + $('.agree-btn').attr('data-cid'),
async: true,
timeout: 15000,
cache: false,
success: data => {
const re = /\d/;
if (!re.test(data)) return false;
$('.agree-num').html('赞 ' + data);
// 创建点赞提示的元素
$('body').append('<span id="agree-p" role="alert">赞 + 1</span>');
// 设置点赞提示的样式
$('#agree-p').css({
top: $('.agree-btn').offset().top - 25,
left: $('.agree-btn').offset().left + $('.agree-btn').outerWidth() / 2 - $('#agree-p').outerWidth() / 2
});
// 让点赞提示消失
$('#agree-p').animate({
top: $('.agree-btn').offset().top - 70,
opacity: 0
}, 400, function () {
$('#agree-p').remove();
});
},
error: () => {
$('.agree-btn').get(0).disabled = false;
}
});
});
}
}
// 头像样式初始化
function avatarStyleInit() {
// 头像加载完成后删除头像背景颜色
if ($('.avatar').length) {
$('.avatar').on('load', ev => {
$(ev.target).css('background-color', 'none');
});
}
// 给评论者头像添加错误事件
for (let i = 0;i < $('.avatar').length;i ++) {
// 检测是否是图片
if ($('.avatar').eq(i)[0].tagName === 'IMG') {
$('.avatar').eq(i).on('error', ev => {
// 获取头像昵称
const name = $(ev.target).attr('alt');
// 创建文字头像元素
const avatarEl = document.createElement('div');
avatarEl.setAttribute('role', 'img');
avatarEl.setAttribute('aria-label', name);
// 设置文字头像的 class
avatarEl.className = 'pingback avatar';
// 把文字头像的内容设置为评论者昵称的第一个字
avatarEl.innerHTML = name.substring(0, 1);
// 检测是否重复出现
const nameIndex = avatarName.indexOf(name);
if (nameIndex === -1) {
avatarName.push(name);
// 生成随机颜色
const bgColor = {r: rand(250, 1), g: rand(250, 1), b: rand(250, 1)};
// 把颜色添加到数组,遇到同名的头像可以使用同一组颜色
avatarColor.push(bgColor);
// 设置文字头像的背景颜色
avatarEl.style.background = 'rgb(' + bgColor.r + ',' + bgColor.g + ',' + bgColor.b + ')';
}else {
// 设置文字头像的背景颜色
avatarEl.style.background = 'rgb(' + avatarColor[nameIndex].r + ',' + avatarColor[nameIndex].g + ',' + avatarColor[nameIndex].b + ')';
}
// 把文字头像插入到页面
$(ev.target).before(avatarEl);
// 移除加载失败的头像
$(ev.target).remove();
});
}
}
// 给独立页友情链接的网站 Logo 添加加载错误事件
$('.page-links .logo').on('error', ev => {
// 创建默认网站 Logo
const logoEl = '<div role="img" class="logo-icon mr-2"><i class="icon-link"></i></div>';
// 把默认网站 Logo 插入到页面
$(ev.target).before(logoEl);
// 移除加载失败的网站 Logo
$(ev.target).remove();
});
}
// 生成随机数的函数
function rand(max, min) {
const num = max - min;
return Math.round(Math.random() * num + min);
}
// 文章目录跳转样式
function directoryStyleInit() {
$('.directory-link').on('click', ev => {
ev.preventDefault();
const titleSelect = `[data-title="${$(ev.target).attr('data-directory')}"]`;
$('html').animate({
scrollTop: $(titleSelect).offset().top - 60
}, 400);
return false;
});
// 如果开启了移动设备文章目录就给目录添加事件
if ($('#directory-mobile').length) {
// 重置目录状态
if (directory) directory = false;
// 移动设备的目录按钮点击
$('#directory-btn').on('click', () => {
if (!directory) {
$('#directory-mobile').css('display', 'flex');
$('#directory-mobile').animate({opacity: 1}, 250);
directory = true;
}else {
$('#directory-mobile').animate({opacity: 0}, 250, () => {
$('#directory-mobile').hide();
});
directory = false;
}
$('#directory-btn').attr('aria-expanded', directory);
});
// 移动设备的关闭目录按钮点击
$('#directory-mobile .close-btn').on('click', () => {
$('#directory-btn').click();
});
}
}
// 调整侧边栏章节目录的尺寸
function directorySize() {
if ($('.sidebar .directory').length) {
// 获取侧边栏章节目录的位置
directoryTop = $('.sidebar .directory').offset().top;
// 设置侧边栏章节目录的最大高度
$('.sidebar .directory').css('max-height', window.innerHeight - 100);
$('.sidebar .directory > .article-directory').css('width', $('.sidebar .directory').width());
}
}
// 分页链接初始化
function paginationLinkInit() {
if ($('.pagination li').length) {
$('.pagination li').addClass('page-item');
$('.pagination li a').addClass('page-link');
$('.pagination .active a').attr('aria-current', 'page');
if ($('.pagination .prev').length) {
$('.pagination .prev a').attr({
'aria-label': '上一页(左光标键)',
'title': '上一页(左光标键)',
'data-toggle': 'tooltip',
'data-placement': 'top'
});
}
if ($('.pagination .next').length) {
$('.pagination .next a').attr({
'aria-label': '下一页(右光标键)',
'title': '下一页(右光标键)',
'data-toggle': 'tooltip',
'data-placement': 'top'
});
}
}else {
$('.page-nav').remove();
}
}
// 图片灯箱初始化
function imageLightboxInit() {
let imgWH = ''; // 记录图片的宽高
let imgDirection = 0; // 图片方向
let contentImgSize = null; // 文章区域的图片尺寸
$('.post-content img').on('click', ev => {
// 如果图片还没有加载完成
if ($(ev.target).attr('class') === 'load-img') return false;
// 获取图片的真实尺寸
const imgSize = {
w: ev.target.naturalWidth,
h: ev.target.naturalHeight
};
// 获取文章内的图片尺寸
contentImgSize = {
w: $(ev.target).width(),
h: $(ev.target).height(),
l: $(ev.target).offset().left,
t: $(ev.target).offset().top
};
// 如果图片的真实尺寸超出屏幕尺寸就重新设置大图的尺寸
if (imgSize.w > window.innerWidth) {
imgSize.p = imgSize.h / imgSize.w * 100;
imgSize.w = window.innerWidth;
imgSize.h = imgSize.w * imgSize.p / 100;
}
if (imgSize.h > window.innerHeight) {
imgSize.p = imgSize.w / imgSize.h * 100;
imgSize.h = window.innerHeight;
imgSize.w = imgSize.h * imgSize.p / 100;
}
// 图片灯箱HTML
const maxImgTemplate = `
<div id="max-img-box" role="dialog" aria-modal="true" aria-labelledby="img-info">
<div id="max-img-bg"></div>
<div class="btn-group max-img-features-btn">
<button type="button" class="btn big" aria-label="放大" title="放大">
<i class="icon-zoom-in"></i>
</button>
<button type="button" class="btn small" aria-label="缩小" title="缩小">
<i class="icon-zoom-out"></i>
</button>
<button type="button" class="btn spin-left" aria-label="左旋转90度" title="左旋转90度">
<i class="icon-undo"></i>
</button>
<button type="button" class="btn spin-right" aria-label="右旋转90度" title="右旋转90度">
<i class="icon-redo"></i>
</button>
<button type="button" class="btn hide-img" aria-label="关闭大图" title="关闭大图">
<i class="icon-cancel-circle"></i>
</button>
</div>
<img src="" alt="大图" class="shadow" id="max-img">
<p id="img-info" class="text-light text-center"></p>
</div>
`;
// 把图片灯箱插入到页面
$('body').append(maxImgTemplate);
// 显示大图
$('#max-img-box').show();
// 显示半透明背景
$('#max-img-bg').fadeIn(250);
// 设置大图的初始尺寸和位置
$('#max-img').css({
display: 'inline',
width: contentImgSize.w,
height: contentImgSize.h,
top: contentImgSize.t,
left: contentImgSize.l
});
// 把大图移动到屏幕中心
$('#max-img').animate({
width: imgSize.w,
height: imgSize.h,
left: window.innerWidth / 2 - imgSize.w / 2,
top: $(document).scrollTop() + window.innerHeight / 2 - imgSize.h / 2
}, 250, 'linear', () => {
// 显示图片操作按钮
$('.max-img-features-btn').css('display', 'flex');
// 让关闭图片的按钮获取焦点
$('.max-img-features-btn .hide-img').focus();
// 显示和设置图片描述
$('#img-info').show();
$('#img-info').html($(ev.target).attr('alt'));
// 把图片灯箱的状态设置为开启
maxImg = true;
});
// 设置大图的 src 和 alt
$('#max-img').attr({
src: $(ev.target).attr('src'),
alt: $(ev.target).attr('alt')
});
// 把图片角度设置为默认
if (imgDirection !== 0) {
imgDirection = 0;
$('#max-img').css('transform', 'rotate(' + imgDirection + 'deg)');
}
// 禁止滚动
$('html').addClass('stop-scrolling');
// 给图片灯箱添加事件
// 在图片灯箱开启的情况下滑动屏幕禁止页面滚动
$('#max-img-bg, .max-img-features-btn, #img-info').on('touchmove', ev => {
if (maxImg) {
ev.preventDefault();
return false;
}
});
// 大图手指拖拽
$('#max-img').on('touchstart', ev => {
const X = ev.touches[0].pageX - $(ev.target).get(0).offsetLeft;
const Y = ev.touches[0].pageY - $(ev.target).get(0).offsetTop;
$(document).on('touchmove', ev => {
$('#max-img').css({
left: ev.touches[0].pageX - X,
top: ev.touches[0].pageY - Y
});
});
$(document).on('touchend', () => {
$(document).off('touchmove');
});
return false;
});
// 大图拖拽
$('#max-img').on('mousedown', ev => {
const X = ev.clientX - $(ev.target).get(0).offsetLeft;
const Y = ev.clientY - $(ev.target).get(0).offsetTop;
$(document).on('mousemove', ev => {
$('#max-img').css({
left: ev.clientX - X,
top: ev.clientY - Y
});
});
$(document).on('mouseup', ev => {
$(document).off('mousemove');
});
return false;
});
// 大图左旋转
$('.max-img-features-btn .spin-left').on('click', () => {
imgDirection -= 90;
$('#max-img').css('transition', '0.3s');
$('#max-img').css('transform', `rotate(${imgDirection}deg)`);
setTimeout(function () {
$('#max-img').css('transition', '0s');
}, 300);
});
// 大图右旋转
$('.max-img-features-btn .spin-right').on('click', () => {
imgDirection += 90;
$('#max-img').css('transition', '0.3s');
$('#max-img').css('transform', `rotate(${imgDirection}deg)`);
setTimeout(function () {
$('#max-img').css('transition', '0s');
}, 300);
});
// 图片放大
$('.max-img-features-btn .big').on('click', () => {
$('#max-img').animate({
width: $('#max-img').width() + $('#max-img').width() / 5,
height: $('#max-img').height() + $('#max-img').height() / 5
}, 250);
});
// 图片缩小
$('.max-img-features-btn .small').on('click', () => {
// 如果图片的宽度或高度 < 40px 将不再缩小
if ($('#max-img').width() <= 80 || $('#max-img').height() <= 80) return false;
$('#max-img').animate({
width: $('#max-img').width() - $('#max-img').width() / 5,
height: $('#max-img').height() - $('#max-img').height() / 5
}, 250);
});
// 大图鼠标滚动
$('#max-img').on('mousewheel DOMMouseScroll', ev => {
if (!maxImg) return false;
if (ev.originalEvent.wheelDelta === undefined) return false;
if (ev.originalEvent.wheelDelta > 0) {
// 放大图片
$('.max-img-features-btn .big').click();
}else {
// 缩小图片
$('.max-img-features-btn .small').click();
}
});
// 大图的关闭按钮点击
$('.max-img-features-btn .hide-img').on('click', () => {
maxImg = false;
// 隐藏半透明背景
$('#max-img-bg').fadeOut(250);
// 隐藏图片描述
$('#img-info').hide();
// 隐藏图片功能区按钮
$('.max-img-features-btn').hide();
$('html').removeClass('stop-scrolling');
$('#max-img').animate({
width: contentImgSize.w,
height: contentImgSize.h,
top: contentImgSize.t,
left: contentImgSize.l
}, 250, 'linear', () => {
$('#max-img').hide();
$('#max-img').attr({
src: '',
alt: ''
});
$('#max-img-box').hide();
$('#max-img-box').remove();
});
});