-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reveal.js
115 lines (101 loc) · 2.95 KB
/
Reveal.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
(function() {
window.addEventListener("load", function() {
var elems = document.getElementsByClassName("reveal");
for (var i = 0; i < elems.length; i++) {
init(elems[i]);
}
});
document.addEventListener("touchmove", drag, {
passive: false
});
document.addEventListener("mousemove", drag);
document.addEventListener("touchend", stop);
document.addEventListener("mouseup", stop);
function drag(evt) {
if (!window.Reveal.currentDrag) {
return;
}
evt.preventDefault();
var x = evt.clientX;
if (evt.touches && evt.touches.length > 0) {
x = evt.touches[0].clientX;
}
window.Reveal.currentDrag.x = x;
window.Reveal.currentDrag.divider = (Math.max(window.Reveal.currentDrag.rect.left, Math.min(window.Reveal.currentDrag.rect.right, x)) - window.Reveal.currentDrag.rect.left) / window.Reveal.currentDrag.rect.width;
window.Reveal.currentDrag.update();
}
function stop() {
if (window.Reveal.currentDrag) {
window.Reveal.currentDrag.update(true);
window.Reveal.currentDrag = null;
}
}
function init(elem) {
if (elem.className.indexOf("reveal-loaded") !== -1) {
return;
}
var state = {
items: [elem.children[0], elem.children[1]],
divider: 0.5,
lastX: -1000,
x: 0,
rect: elem.getBoundingClientRect(),
update: update
};
var supportsClipPath = true;
window.requestAnimationFrame(function() {
if (!state.items[1].style.clipPath) {
supportsClipPath = false;
update(true);
}
});
state.items[0].className += " reveal-img";
state.items[1].className += " reveal-img";
elem.className += " reveal-loaded";
var revealBar = createRevealBar();
update(true);
revealBar.addEventListener("touchstart", start);
revealBar.addEventListener("mousedown", start);
elem.appendChild(revealBar);
function start() {
state.rect = elem.getBoundingClientRect();
window.Reveal.currentDrag = state;
}
function update(force) {
var percent = state.divider * 100;
revealBar.style.left = percent + "%";
if (Math.abs(state.x - state.lastX) < 5 && !force) {
return;
}
if (window.Reveal.onupdate) {
window.Reveal.onupdate({
elem: elem,
percent: percent
});
}
state.lastX = state.x;
if (!supportsClipPath) {
state.items[1].style.clip = "rect(0 " + state.rect.width + "px " + state.rect.height + "px " + state.divider * state.rect.width + "px)";
} else {
state.items[1].style.clipPath = "inset(0 0 0 " + percent + "%)";
}
}
}
window.Reveal = {
currentDrag: null,
init: init,
onupdate: null
};
function createRevealBar() {
var revealBar = document.createElement("div");
revealBar.className = "reveal-bar";
var revealGrabber = document.createElement("div");
revealGrabber.className = "reveal-grabber";
var revealArrows = document.createElement("div");
revealArrows.className = "reveal-arrows";
revealArrows.innerHTML = "◄ ►";
revealGrabber.appendChild(revealArrows);
revealBar.appendChild(revealGrabber);
return revealBar;
}
})();