-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-gallery.js
208 lines (178 loc) · 4.74 KB
/
image-gallery.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
var ImageGallery = function(options)
{
this.options = options || {
'container': '.fs-gallery-container',
'previewClass': '.fs-gallery-preview',
'thumbContainer': '.fs-gallery-thumb-container',
'thumbClass': '.fs-gallery-thumb',
'overlayClass': 'fs-gallery-overlay',
'modalClass': 'fs-gallery-modal'
};
this.previewIndex = 0;
this.numberOfPreview = this.countPreview();
this.setupIndexes();
this.setupBindings();
};
ImageGallery.prototype.countPreview = function()
{
return $(this.options.thumbClass).length;
};
ImageGallery.prototype.setupIndexes = function()
{
var index;
for (index = 0; index < $(this.options.thumbClass).length; index++) {
$($(this.options.thumbClass)[index]).find('img').attr('data-index', index);
}
};
ImageGallery.prototype.setupBindings = function()
{
var _self = this;
// Thumbnail click
_self.registerThumbnailClick();
// Preview click
_self.registerPreviewClick();
};
ImageGallery.prototype.registerThumbnailClick = function()
{
var _self = this;
$(_self.options.thumbClass).on('click', function()
{
_self.previewIndex = $(this).find('img').data('index');
$(_self.options.previewClass).find('img').attr('src', $(this).find('img').data('preview-image'));
});
};
ImageGallery.prototype.registerPreviewClick = function()
{
var _self = this;
$(_self.options.previewClass).on('click', function()
{
_self.createOverlay();
var _image = '<img src="' + $(this).find('img').attr('src') + '">';
$('.' + _self.options.modalClass).html(_image);
});
};
ImageGallery.prototype.createOverlay = function()
{
if ($('.' + this.options.overlayClass).length == 0) {
var _modal = document.createElement('div');
_modal.className = this.options.modalClass;
var _overlay = document.createElement('div');
_overlay.className = this.options.overlayClass;
_overlay.appendChild(_modal);
document.body.appendChild(_overlay);
this.createNavigation();
this.registerScrollFix();
}
};
ImageGallery.prototype.createNavigation = function()
{
var _close = document.createElement('div');
_close.className = 'fs-navigation fs-close';
document.body.appendChild(_close);
if ($(this.options.thumbContainer + ' li').length > 1) {
var _left = document.createElement('div');
_left.className = 'fs-navigation fs-left';
document.body.appendChild(_left);
var _right = document.createElement('div');
_right.className = 'fs-navigation fs-right';
document.body.appendChild(_right);
}
this.registerNavigation();
};
ImageGallery.prototype.registerScrollFix = function()
{
var _self = this;
$('.' + this.options.modalClass).on('DOMMouseScroll mousewheel', function(ev) {
var $this = $(this),
scrollTop = this.scrollTop,
scrollHeight = this.scrollHeight,
height = $this.height(),
delta = ev.originalEvent.wheelDelta,
up = delta > 0;
var prevent = function() {
ev.stopPropagation();
ev.preventDefault();
ev.returnValue = false;
return false;
}
if (!up && -delta > scrollHeight - height - scrollTop) {
$this.scrollTop(scrollHeight);
return prevent();
} else if (up && delta > scrollTop) {
$this.scrollTop(0);
return prevent();
}
});
};
ImageGallery.prototype.registerNavigation = function()
{
var _self = this;
// Close overlay
$(document).keyup(function(e)
{
if (e.keyCode == 27) {
_self.destroyOverlay();
}
});
$('.fs-navigation.fs-close').on('click', function() {
_self.destroyOverlay();
});
// Preview next
$('.fs-right').on('click', function() {
_self.previewNext();
});
$(document).keyup(function(e)
{
if (e.keyCode == 39) {
_self.previewNext();
}
});
// Preview previous
$('.fs-left').on('click', function() {
_self.previewPrevious();
});
$(document).keyup(function(e)
{
if (e.keyCode == 37) {
_self.previewPrevious();
}
});
};
ImageGallery.prototype.previewNext = function()
{
var _self = this;
var index = _self.previewIndex;
if (_self.previewIndex + 1 < _self.numberOfPreview) {
index = _self.previewIndex + 1; // One forward
} else {
index = 0; // Select first
}
$(_self.options.thumbClass)[index].click();
$(_self.options.previewClass).click();
};
ImageGallery.prototype.previewPrevious = function()
{
var _self = this;
var index = _self.previewIndex;
if (_self.previewIndex - 1 >= 0) {
index = _self.previewIndex - 1; // One back
} else {
index = _self.numberOfPreview - 1; // Select last
}
$(_self.options.thumbClass)[index].click();
$(_self.options.previewClass).click();
};
ImageGallery.prototype.destroyOverlay = function()
{
this.destroyNavigation();
$('.' + this.options.overlayClass).fadeOut(250, function() {
this.remove();
});
};
ImageGallery.prototype.destroyNavigation = function()
{
$('.fs-navigation').remove();
$('.fs-left').off('click');
$('.fs-right').off('click');
$(document).unbind('keyup');
};