-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathspoiler.js
84 lines (72 loc) · 2.36 KB
/
spoiler.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
(function() {
if (typeof Object.assign != 'function') {
(function () {
Object.assign = function (target) {
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var output = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== undefined && source !== null) {
for (var nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
};
})();
}
window.spoilerAlert = function(selector, opts) {
var elements = document.querySelectorAll(selector);
var defaults = {
max: 4,
partial: 2,
hintText: 'Click to reveal completely'
};
opts = Object.assign(defaults, opts || {});
var maxBlur = opts.max;
var partialBlur = opts.partial;
var hintText = opts.hintText;
var processElement = function(index) {
var el = elements[index];
el['data-spoiler-state'] = 'shrouded';
el.style.webkitTransition = '-webkit-filter 250ms';
el.style.transition = 'filter 250ms';
var applyBlur = function(radius) {
el.style.filter = 'blur('+radius+'px)';
el.style.webkitFilter = 'blur('+radius+'px)';
}
applyBlur(maxBlur);
el.addEventListener('mouseover', function(e) {
el.style.pointer = 'Cursor';
el.title = hintText;
if (el['data-spoiler-state'] === 'shrouded') applyBlur(partialBlur);
})
el.addEventListener('mouseout', function(e) {
el.title = hintText;
if (el['data-spoiler-state'] === 'shrouded') applyBlur(maxBlur);
})
el.addEventListener('click', function(e) {
switch(el['data-spoiler-state']) {
case 'shrouded':
el['data-spoiler-state'] = 'revealed';
el.title = '';
el.style.cursor = 'auto';
applyBlur(0);
break;
default:
el['data-spoiler-state'] = 'shrouded';
el.title = hintText;
el.style.cursor = 'pointer';
applyBlur(maxBlur);
}
})
}
for (var i = 0; i !== elements.length; i++) processElement(i);
}
})();