-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
247 lines (213 loc) · 7.19 KB
/
Copy pathindex.js
File metadata and controls
247 lines (213 loc) · 7.19 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
$(document).ready(function(){
var nav = $('.g-nav');
/**
* Responsive Navigation
*/
$('#menu-toggle').on('click', function(e) {
var duration = 200;
nav.slideToggle(duration);
$(document).on('click', function() {
nav.slideUp(duration);
});
e.stopPropagation();
});
nav.on('click', function(e) {
e.stopPropagation();
});
/*
* Header Bar
*/
if($(window).width() > 695) {
var header = $('.g-header');
var headerHeight = header.outerHeight();
var logo = $('.g-logo');
var navText = nav.find('a');
var themeStyle = $('.g-banner').attr('data-theme');
var scFlag = $(document).scrollTop();
$(document).scroll(function() {
var scrollTop = $(this).scrollTop();
var navClassName = 'nav-' + themeStyle;
if (scrollTop > headerHeight) {
if(scrollTop > 3 * headerHeight) {
header.addClass('headerUp');
}
header.css({
'background-color': 'rgba(255, 255, 255, .98)',
'box-shadow': '0 1px 12px rgba(0, 0, 0, .08)'
});
logo.css({
'background': 'url(/assets/icons/logo_' + themeStyle + '.svg) no-repeat center',
'background-size': '100% 100%'
});
navText.css('color', '#666');
nav.addClass(navClassName);
} else {
header.removeClass('headerUp');
header.css({
'background-color': 'transparent',
'box-shadow': 'none'
});
logo.css({
'background': 'url(/assets/icons/logo.svg) no-repeat center',
'background-size': '100% 100%'
});
navText.css('color', '#fff');
nav.removeClass(navClassName);
}
// scroll action
if (scFlag > scrollTop) {
header.addClass('headerDown');
} else {
header.removeClass('headerDown');
}
scFlag = scrollTop;
});
}
/*
* Post Cover Resize
*/
function postCover(img, container) {
var imgWidth = img.width();
var containerWidth = container.width();
var imgHeight = img.height();
var containerHeight = container.height();
if (imgHeight < containerHeight) {
img.css({
'width': 'auto',
'height': '100%'
});
imgWidth = img.width(),
containerWidth = container.width();
var marginLeft = (imgWidth - containerWidth) / 2;
img.css('margin-left', '-' + marginLeft + 'px');
} else {
var marginTop = (containerHeight - imgHeight) / 2;
img.css('margin-top', marginTop + 'px');
}
img.fadeIn();
}
/**
* The Post Navigator
*/
$('.read-next-item section').each(function() {
var n = $(this).height();
var rn = $('.read-next-item').height();
$(this).css('margin-top', (rn - n) / 2 + 'px');
$(this).fadeIn();
});
$('.read-next-item img').each(function(){
postCover($(this), $('.read-next-item'));
});
/**
* Pagination
*/
function pagination() {
var total = parseInt($('#total_pages').val());
var current = parseInt($('#current_pages').val());
var baseUrl = $('#base_url').val();
var limit = 3;
var link_html = '';
for (var i = current - limit; i < current; i++) {
if (i > 0 && i !== 1) {
link_html += '<a href="' + baseUrl + 'page' + i + '" class="page-link page-num">' + i + '</a>';
} else if (i === 1) {
link_html += '<a href="' + baseUrl + '" class="page-link page-num">' + i + '</a>';
}
}
link_html += '<span class="page-link page-num active">' + current + '</span>';
for (var j = current + 1; j <= current + limit; j++) {
if (j <= total) {
link_html += '<a href="' + baseUrl + 'page' + j + '" class="page-link page-num">' + j + '</a>';
}
}
$('#page-link-container').html(link_html);
}
pagination();
/**
* Search
*/
function Search() {
var self = this;
var input = $('#search_input');
var result = $('.search_result');
input.focus(function() {
$('.icon-search').css('color', '#3199DB');
result.show();
});
input.keyup(debounce(this.autoComplete));
$(document).click(function(e) {
if (e.target.id !== 'search_input' || e.target.className !== 'search_result' || e.target.className !== 'search_item') {
$('.icon-search').css('color', '#CAD3DC');
result.hide();
}
});
}
Search.prototype.autoComplete = function() {
var keywords = this.value.toLowerCase();
if (keywords.length) {
$('.icon-search').css('color', '#3199DB');
} else {
$('.icon-search').css('color', '#CAD3DC');
}
$.getJSON('../../search.json').done(function(data) {
var html = '';
for (var i in data) {
var item = data[i];
var title = item.title;
var tags = item.tags;
var url = item.url;
var k = title + tags;
if (keywords !== '' && k.toLowerCase().indexOf(keywords) >= 0) {
html += '<a class="search_item" href="' + item.url + '">' + item.title + '</a>';
}
}
$('.search_result').html(html);
});
};
function debounce(fn, delay) {
var timer;
delay = delay || 120;
return function () {
var ctx = this;
var args = arguments;
var later = function() {
fn.apply(ctx, args);
};
clearTimeout(timer);
timer = setTimeout(later, delay);
};
}
new Search();
/**
* Night mode
*/
function nightMode() {
var el = $('body');
var className = 'night-mode';
var date = new Date();
var hour = date.getHours();
if ((hour >= 0 && hour <= 6) || hour === 23) {
el.addClass(className);
}
}
if ($('#nm-switch').val() === 'true') {
nightMode();
}
/**
* Copy and copyright
*/
function setClipboardData(str) {
str += '\n\n著作权归作者所有。\n商业转载请联系作者获得授权,非商业转载请注明出处。\n原文: ' + location.href;
$('.post-content').on('copy', function(e) {
var data = window.clipboardData || e.originalEvent.clipboardData;
data.setData('text/plain', str);
e.preventDefault();
});
}
$('.post-content').on('mouseup', function(e) {
var txt = window.getSelection();
if (txt.toString().length >= 30) {
setClipboardData(txt);
}
});
});