-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzoomtastic.js
184 lines (156 loc) Β· 5.32 KB
/
zoomtastic.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
// Initialization status
let mounted = false;
// Locking clicking on images
let locked = false;
// Elements
let container;
let background;
let image;
/**
* @typedef {object} ZoomtasticConfig Zoomtastic configuration.
* @property {string} [size] Image size.
* @property {string} [ease] Timing function.
* @property {number} [duration] Animations duration.
* @property {string} [background] Viewer background.
* @property {string} [filter] CSS filter applied to image.
* @property {'slide'|'fade'|'zoom'|'drop'} [animation] Animation type.
*/
/**
* Zoomtastic π
* Tiny image viewer for web!
*/
const Zoomtastic = { config: {} };
/**
* Mount Zoomtastic element to the page.
* @param {ZoomtasticConfig} config Zoomtastic configuration.
*/
Zoomtastic.mount = function (config = {}) {
const existingContainer = document.getElementById('zoomtastic-container');
if (existingContainer) existingContainer.remove();
Zoomtastic.config.size = config.size || '95%';
Zoomtastic.config.easing = config.easing || 'ease';
Zoomtastic.config.duration = config.duration || 300;
Zoomtastic.config.background = config.background || 'rgba(0, 0, 0, 0.9)';
Zoomtastic.config.filter = config.filter || 'drop-shadow(0 2px 16px rgba(0, 0, 0, 0.3))';
Zoomtastic.config.animation = config.animation || 'slide';
container = createElement('zoomtastic-container', {
top: '0',
left: '0',
width: '100%',
height: '100vh',
display: 'flex',
position: 'fixed',
alignItems: 'center',
justifyContent: 'center',
maxHeight: '100dvh',
cursor: 'zoom-out',
zIndex: '16777271',
visibility: 'hidden',
});
background = createElement('zoomtastic-background', {
width: '100%',
height: '100%',
zIndex: '0',
opacity: '0',
userSelect: 'none',
position: 'absolute',
background: Zoomtastic.config.background,
transitionProperty: 'opacity',
transitionTimingFunction: Zoomtastic.config.easing,
transitionDuration: parseInt(Zoomtastic.config.duration * 0.75) + 'ms',
});
image = createElement('zoomtastic-image', {
width: Zoomtastic.config.size,
height: Zoomtastic.config.size,
opacity: '0',
zIndex: '16777271',
userSelect: 'none',
pointerEvents: 'none',
backgroundSize: 'contain',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
transitionProperty: 'all',
transitionTimingFunction: Zoomtastic.config.easing,
transitionDuration: parseInt(Zoomtastic.config.duration) + 'ms',
transform: 'translateY(0) scale(1)',
filter: Zoomtastic.config.filter,
});
if (Zoomtastic.config.animation === 'slide') image.style.transform = 'translateY(5%) scale(1)';
if (Zoomtastic.config.animation === 'zoom') image.style.transform = 'scale(0.95)';
if (Zoomtastic.config.animation === 'drop') image.style.transform = 'scale(1.1)';
container.addEventListener('click', () => {
if (locked) return;
locked = true;
Zoomtastic.hide();
});
container.appendChild(image);
container.appendChild(background);
document.body.appendChild(container);
locked = false;
mounted = true;
};
/**
* Add event listener to the elements. By default, it listens to all elements with the attribute `zoomtastic` and takes the image from the `src` attribute.
* @param {string|HTMLElement|HTMLCollection|NodeList} [target='[zoomtastic]'] CSS selector, element or array of elements.
* @param {string} [source='src'] Image source.
*/
Zoomtastic.listen = function (target = '[zoomtastic]', source = 'src') {
if (!mounted) Zoomtastic.mount();
if (typeof target === 'string') target = document.querySelectorAll(target);
if (target instanceof HTMLElement) target = [target];
if (!target) return;
for (let i = 0; i < target.length; i++) {
const element = target[i];
element.style.cursor = 'zoom-in';
element.addEventListener('click', event => {
event.preventDefault();
const url = element.getAttribute(source);
if (url) Zoomtastic.show(url);
});
}
};
/**
* Show image viewer.
* @param {string} url URL to the image.
*/
Zoomtastic.show = function (url) {
if (!url) throw new TypeError('URL is not specified');
if (!mounted) Zoomtastic.mount();
image.style.backgroundImage = `url("${encodeURI(url)}")`;
container.style.visibility = 'visible';
setTimeout(() => {
if (Zoomtastic.config.animation === 'slide') image.style.transform = 'translateY(0) scale(1)';
if (Zoomtastic.config.animation === 'zoom' || Zoomtastic.config.animation === 'drop') image.style.transform = 'translateY(0) scale(1)';
image.style.opacity = '1';
background.style.opacity = '1';
locked = false;
});
};
/**
* Hide image viewer.
*/
Zoomtastic.hide = function () {
if (!mounted) Zoomtastic.mount();
setTimeout(() => {
if (Zoomtastic.config.animation === 'slide') image.style.transform = 'translateY(5%) scale(1)';
if (Zoomtastic.config.animation === 'zoom') image.style.transform = 'translateY(0) scale(0.95)';
if (Zoomtastic.config.animation === 'drop') image.style.transform = 'translateY(0) scale(1.1)';
image.style.opacity = '0';
background.style.opacity = '0';
});
setTimeout(() => {
image.style.backgroundImage = 'none';
container.style.visibility = 'hidden';
locked = false;
}, parseInt(Zoomtastic.config.duration));
};
/**
* Create new element.
*/
function createElement(id, styles = {}) {
const element = document.createElement('div');
for (const key in styles) element.style[key] = styles[key];
element.id = id;
return element;
}
export default Zoomtastic;