-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkico.js
444 lines (359 loc) · 13.3 KB
/
kico.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
/* ----
# Kico Style 1.0
# By: Dreamer-Paul
# Last Update: 2022.6.29
一个可口的极简响应式前端框架。
本代码为奇趣保罗原创,并遵守 MIT 开源协议。欢迎访问我的博客:https://paugram.com
---- */
Array.prototype.remove = function (value) {
var index = this.indexOf(value);
if(index > -1) this.splice(index, 1);
};
(function (global, setting) {
var KStyle = function (a, b) {
return KStyle.fn.init(a, b);
};
KStyle.fn = KStyle.prototype = {
construtor: KStyle,
init: function (a, b) {
a = KStyle.selectAll(a);
a.each = function (fn){
return KStyle.each(a, fn);
};
a.image = function () {
return KStyle.image(a);
};
a.lazy = function (bg) {
return KStyle.lazy(a, bg);
};
a.scrollTo = function (offset) {
return KStyle.scrollTo(a, offset);
};
a.empty = function () {
return KStyle.each(a, function (item) { KStyle.empty(item); });
}
return a;
}
};
// 批量处理
KStyle.each = function (data, fn) {
for(var i = 0; i < data.length; i++){
fn(data[i], i, data);
}
};
// 创建对象
KStyle.create = function (tag, prop) {
var obj = document.createElement(tag);
if(prop){
if(prop.id) obj.id = prop.id;
if(prop.src) obj.src = prop.src;
if(prop.href) obj.href = prop.href;
if(prop.class) obj.className = prop.class;
if(prop.text) obj.innerText = prop.text;
if(prop.html) obj.innerHTML = prop.html;
if(prop.child){
if(prop.child.constructor === Array){
KStyle.each(prop.child, (i) => {
obj.appendChild(i);
});
}
else{
obj.appendChild(prop.child);
}
}
if(prop.attr){
if(prop.attr.constructor === Array){
KStyle.each(prop.attr, (i) => {
obj.setAttribute(i.name, i.value);
});
}
else if(prop.attr.constructor === Object){
obj.setAttribute(prop.attr.name, prop.attr.value);
}
}
if(prop.parent) prop.parent.appendChild(obj);
}
return obj;
};
// 选择对象
KStyle.select = function (obj) {
switch(typeof obj){
case "object": return obj; break;
case "string": return document.querySelector(obj); break;
}
};
KStyle.selectAll = function (obj) {
switch(typeof obj){
case "object": return obj; break;
case "string": return document.querySelectorAll(obj); break;
}
};
// 清空子元素
KStyle.empty = function (obj) {
while(obj.firstChild){
obj.removeChild(obj.firstChild);
}
}
// 弹窗
var notice = {
wrap: KStyle.create("notice"),
list: []
};
KStyle.notice = function (content, attr) {
var item = KStyle.create("div", {class: "ks-notice", html: "<span class='content'>" + content + "</span>", parent: notice.wrap});
notice.list.push(item);
if(!document.querySelector("body > notice")) document.body.appendChild(notice.wrap);
if(attr && attr.time){
setTimeout(notice_remove, attr.time);
}
else{
var close = KStyle.create("span", {class: "close", parent: item});
close.onclick = notice_remove;
}
if(attr && attr.color){
item.classList.add(attr.color);
}
function notice_remove() {
item.classList.add("remove");
notice.list.remove(item);
setTimeout(function () {
try{
notice.wrap.removeChild(item);
item = null;
}
catch(err) {}
if(document.querySelector("body > notice") && notice.list.length === 0){
document.body.removeChild(notice.wrap);
}
}, 300);
}
};
// 灯箱
var image_box = {
img: KStyle.create("img"),
vid: KStyle.create("video"),
prev: KStyle.create("div", {class: "ks-prev"}),
next: KStyle.create("div", {class: "ks-next"}),
ball: KStyle.create("div", {class: "ks-ball"})
};
image_box.wrap = KStyle.create("div", {class: "ks-image", child: [
image_box.prev, image_box.img, image_box.vid, image_box.next, image_box.ball
]});
image_box.wrap.onclick = function (e) {
image_box.wrap.classList.add("remove");
setTimeout(function () {
try{
document.body.removeChild(image_box.wrap);
image_box.wrap.classList.remove("remove");
}
catch (err){}
}, 300);
};
image_box.img.alt = "灯箱预览";
image_box.img.onload = function () {
image_box.wrap.classList.remove("loading");
};
image_box.vid.controls = true;
image_box.vid.onclick = function (ev) {
ev.stopPropagation();
}
image_box.vid.onloadeddata = function () {
image_box.wrap.classList.remove("loading");
};
KStyle.image = function (selector) {
var current = 0;
var get_images = KStyle.selectAll(selector);
var actions = {
ori: function (obj, num) {
obj.setAttribute("ks-image", "active");
obj.onclick = function () {
current = num;
actions.set();
document.body.appendChild(image_box.wrap);
};
},
set: function () {
var img = get_images[current];
current === 0 ? image_box.prev.classList.add("ended") : image_box.prev.classList.remove("ended");
current === get_images.length - 1 ? image_box.next.classList.add("ended") : image_box.next.classList.remove("ended");
if(img.getAttribute("ks-original") !== null){
const _url = img.getAttribute("ks-original");
// 视频格式,展示成视频效果
if (_url.includes("mp4")) {
image_box.img.removeAttribute("src");
image_box.vid.src = _url;
}
else {
image_box.img.src = _url;
image_box.vid.removeAttribute("src");
}
}
else if(img.src){
image_box.img.src = img.src;
}
else{
console.error("This image has no valid tag!");
}
image_box.wrap.classList.add("loading");
}
};
KStyle.each(get_images, function (item, id) {
if(item.src || item.getAttribute("ks-original")){
actions.ori(item, id);
}
});
// 设置按钮
image_box.prev.onclick = function (e) {
e.stopPropagation();
if(current - 1 >= 0) current--;
actions.set();
};
image_box.next.onclick = function (e) {
e.stopPropagation();
if(current + 1 < get_images.length) current++;
actions.set();
};
};
// 懒加载
KStyle.lazy = function (elements, bg) {
//elements = Array.from(KStyle.selectAll(elements));
elements = KStyle.selectAll(elements);
var list = [];
var action = {
setFront: function (item) {
if(item.intersectionRatio > 0) {
item.target.src = item.target.link;
item.target.onload = function () {
item.target.setAttribute("ks-lazy", "finished");
}
obs.unobserve(item.target);
}
},
setBack: function (item) {
if(item.boundingClientRect.top <= window.innerHeight + 100) {
var img = new Image();
img.src = item.target.link;
img.onload = function () {
item.target.setAttribute("ks-lazy", "finished");
item.target.style.backgroundImage = "url(" + item.target.link + ")";
};
obs.unobserve(item.target);
}
}
};
// 是否支持 OBS
if(global.IntersectionObserver){
var obs = new IntersectionObserver(function (changes) {
if (bg) {
changes.forEach(function (t) {
action.setBack(t);
});
}
else {
changes.forEach(function (t) {
action.setFront(t);
});
}
});
KStyle.each(elements, function (item) {
item.link = item.getAttribute("ks-thumb") || item.getAttribute("ks-original");
if(!item.getAttribute("ks-lazy")) obs.observe(item);
})
}
else{
function back() {
KStyle.each(list, function (item) {
var check = item.el.getBoundingClientRect().top <= window.innerHeight;
if(check && !item.showed){
action.setBack(item.el);
list.remove(item);
}
});
}
function front() {
KStyle.each(list, function (item) {
var check = item.el.getBoundingClientRect().top <= window.innerHeight;
if(check && !item.showed){
action.setFront(item.el);
list.remove(item);
}
});
}
KStyle.each(elements, function (item) {
item.link = item.getAttribute("ks-thumb") || item.getAttribute("ks-original");
if(!item.getAttribute("ks-lazy")) list.push({el: item, link: item.link});
});
bg ? back() : front();
bg ? document.addEventListener("scroll", back) : document.addEventListener("scroll", front);
}
};
// AJAX
KStyle.ajax = function (prop) {
if(!prop.url) prop.url = document.location.href;
if(!prop.method) prop.method = "GET";
if(prop.method === "POST"){
var data = new FormData();
for(var d in prop.data){
data.append(d, prop.data[d]);
}
}
else if(prop.method === "GET"){
var url = prop.url + "?";
for(var d in prop.data){
url += d + "=" + prop.data[d] + "&";
}
prop.url = url.substr(0, url.length - 1);
}
var request = new XMLHttpRequest();
request.open(prop.method, prop.url);
if(prop.crossDomain){ request.setRequestHeader("X-Requested-With", "XMLHttpRequest"); }
if(prop.header){
for(var i in prop.header){
request.setRequestHeader(prop.header[i][0], prop.header[i][1]);
}
}
request.send(data);
request.onreadystatechange = function () {
if(request.readyState === 4){
if(request.status === 200 || request.status === 304){
if(prop.type){
switch(prop.type){
case "text": prop.success(request.responseText); break;
case "json": prop.success(JSON.parse(request.response)); break;
}
}
else{
prop.success ? prop.success(request) : console.log(prop.method + " 请求发送成功");
}
}
else{
prop.failed ? prop.failed(request) : console.log(prop.method + " 请求发送失败");
}
request = null;
}
};
return request;
};
// 平滑滚动
KStyle.scrollTo = function (el, offset) {
el = KStyle.selectAll(el);
el.forEach(function (t) {
t.onclick = function (e) {
var l = e.target.pathname;
var c = window.location.pathname;
var t = e.target.href.match(/#[\s\S]+/);
if(t) t = ks.select(t[0]);
if(c === l){
e.preventDefault();
var top = t ? (offset ? t.offsetTop - offset : t.offsetTop) : 0;
"scrollBehavior" in document.documentElement.style ? global.scrollTo({top: top, left: 0, behavior: "smooth"}) : global.scrollTo(0, top);
}
else{
console.log(c, l);
}
}
})
};
global.ks = KStyle;
console.log("%c Kico Style %c https://paugram.com ","color: #fff; margin: 1em 0; padding: 5px 0; background: #3498db;","margin: 1em 0; padding: 5px 0; background: #efefef;");
})(window);