forked from manika137/Landing-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
103 lines (82 loc) · 3.2 KB
/
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
// Fit inner div to gallery
$('<div />', { 'class': 'inner' }).appendTo('.gallery');
// Create main image block and apply first img to it
var imageSrc1 = $('.gallery').children('img').eq(0).attr('src');
$('<div />', { 'class': 'main' }).appendTo('.gallery .inner').css('background-image', 'url(' + imageSrc1 + ')');
// Create image number label
var noOfImages = $('.gallery').children('img').length;
$('<span />').appendTo('.gallery .inner .main').html('Image 1 of ' + noOfImages);
// Create thumb roll
$('<div />', { 'class': 'thumb-roll' }).appendTo('.gallery .inner');
// Create thumbail block for each image inside thumb-roll
$('.gallery').children('img').each( function() {
var imageSrc = $(this).attr('src');
$('<div />', { 'class': 'thumb' }).appendTo('.gallery .inner .thumb-roll').css('background-image', 'url(' + imageSrc + ')');
});
// Make first thumbnail selected by default
$('.thumb').eq(0).addClass('current');
// Select thumbnail function
$('.thumb').click(function() {
// Make clicked thumbnail selected
$('.thumb').removeClass('current');
$(this).addClass('current');
// Apply chosen image to main
var imageSrc = $(this).css('background-image');
$('.main').css('background-image', imageSrc);
$('.main').addClass('main-selected');
setTimeout(function() {
$('.main').removeClass('main-selected');
}, 500);
// Change text to show current image number
var imageIndex = $(this).index();
$('.gallery .inner .main span').html('Image ' + (imageIndex + 1) + ' of ' + noOfImages);
});
// Arrow key control function
$(document).keyup(function(e) {
// If right arrow
if (e.keyCode === 39) {
// Mark current thumbnail
var currentThumb = $('.thumb.current');
var currentThumbIndex = currentThumb.index();
if ( (currentThumbIndex+1) >= noOfImages) { // if on last image
nextThumbIndex = 0; // ...loop back to first image
} else {
nextThumbIndex = currentThumbIndex+1;
}
var nextThumb = $('.thumb').eq(nextThumbIndex);
currentThumb.removeClass('current');
nextThumb.addClass('current');
// Switch main image
var imageSrc = nextThumb.css('background-image');
$('.main').css('background-image', imageSrc);
$('.main').addClass('main-selected');
setTimeout(function() {
$('.main').removeClass('main-selected');
}, 500);
// Change text to show current image number
$('.gallery .inner .main span').html('Image ' + (nextThumbIndex+1) + ' of ' + noOfImages);
}
// If left arrow
if (e.keyCode === 37) {
// Mark current thumbnail
var currentThumb = $('.thumb.current');
var currentThumbIndex = currentThumb.index();
if ( currentThumbIndex == 0) { // if on first image
prevThumbIndex = noOfImages-1; // ...loop back to last image
} else {
prevThumbIndex = currentThumbIndex-1;
}
var prevThumb = $('.thumb').eq(prevThumbIndex);
currentThumb.removeClass('current');
prevThumb.addClass('current');
// Switch main image
var imageSrc = prevThumb.css('background-image');
$('.main').css('background-image', imageSrc);
$('.main').addClass('main-selected');
setTimeout(function() {
$('.main').removeClass('main-selected');
}, 500);
// Change text to show current image number
$('.gallery .inner .main span').html('Image ' + (prevThumbIndex+1) + ' of ' + noOfImages);
}
});