-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (123 loc) · 4.67 KB
/
main.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
(function ($) {
// To top button
$("#back-to-top").on('click', function () {
$('body, html').animate({ scrollTop: 0 }, 600);
});
// Nav bar toggle
$('#main-nav-toggle').on('click', function () {
$('.nav-container-inner').slideToggle();
});
// Caption
$('.article-entry').each(function(i) {
$(this).find('img').each(function() {
if (this.alt && !(!!$.prototype.justifiedGallery && $(this).parent('.justified-gallery').length)) {
$(this).after('<span class="caption">' + this.alt + '</span>');
}
// 对于已经包含在链接内的图片不适用lightGallery
if ($(this).parent().prop("tagName") !== 'A') {
$(this).wrap('<a href="' + this.src + '" title="' + this.alt + '" class="gallery-item"></a>');
}
});
});
if (typeof lightGallery != 'undefined') {
var options = {
selector: '.gallery-item',
};
$('.article-entry').each(function(i, entry) {
lightGallery(entry, options);
});
lightGallery($('.article-gallery')[0], options);
}
if (!!$.prototype.justifiedGallery) { // if justifiedGallery method is defined
var options = {
rowHeight: 140,
margins: 4,
lastRow: 'justify'
};
$('.justified-gallery').justifiedGallery(options);
}
// Sidebar expend
$('#sidebar .sidebar-toggle').click(function () {
if($('#sidebar').hasClass('expend')) {
$('#sidebar').removeClass('expend');
} else {
$('#sidebar').addClass('expend');
}
});
// Remove extra main nav wrap
$('.main-nav-list > li').unwrap();
// Highlight current nav item
$('#main-nav > li > .main-nav-list-link').each(function () {
if($('.page-title-link').length > 0){
if ($(this).html().toUpperCase() == $('.page-title-link').html().toUpperCase()) {
$(this).addClass('current');
} else if ($(this).attr('href') == $('.page-title-link').attr('data-url')) {
$(this).addClass('current');
}
}
});
// Auto hide main nav menus
function autoHideMenus(){
var max_width = $('.nav-container-inner').width() - 10;
var main_nav_width = $('#main-nav').width();
var sub_nav_width = $('#sub-nav').width();
if (main_nav_width + sub_nav_width > max_width) {
// If more link not exists
if ($('.main-nav-more').length == 0) {
$(['<li class="main-nav-list-item top-level-menu main-nav-more">',
'<a class="main-nav-list-link" href="javascript:;">More</a>',
'<ul class="main-nav-list-child">',
'</ul></li>'].join('')).appendTo($('#main-nav'));
// Bind hover event
$('.main-nav-more').hover(function () {
if($(window).width() < 480) {
return;
}
$(this).children('.main-nav-list-child').slideDown('fast');
}, function () {
if($(window).width() < 480) {
return;
}
$(this).children('.main-nav-list-child').slideUp('fast');
});
}
var child_count = $('#main-nav').children().length;
for (var i = child_count - 2; i >= 0; i--) {
var element = $('#main-nav').children().eq(i);
if (main_nav_width + sub_nav_width > max_width) {
element.prependTo($('.main-nav-more > ul'));
main_nav_width = $('#main-nav').width();
} else {
return;
}
}
}
// Nav bar is wide enough
if ($('.main-nav-more').length > 0) {
$('.main-nav-more > ul').children().appendTo($('#main-nav'));
$('.main-nav-more').remove();
}
}
autoHideMenus();
$(window).resize(function () {
autoHideMenus();
});
// Fold second-level menu
$('.main-nav-list-item').hover(function () {
if ($(window).width() < 480) {
return;
}
$(this).children('.main-nav-list-child').slideDown('fast');
}, function () {
if ($(window).width() < 480) {
return;
}
$(this).children('.main-nav-list-child').slideUp('fast');
});
// Add second-level menu mark
$('.main-nav-list-item').each(function () {
if ($(this).find('.main-nav-list-child').length > 0) {
$(this).addClass('top-level-menu');
}
});
})(jQuery);