This repository has been archived by the owner on Feb 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
slider.js
121 lines (84 loc) · 3.09 KB
/
slider.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
(function () {
'use strict';
var cssSlider = {
// CSS-Slider selectors
selectors: {
container: 'slider-wrapper',
sliderItems: '.slider-content > li',
firstSlide: '.slider-content > li:first-child',
activeSlide: '.slider-content > .active',
clickFrame: '.slider-clickframe',
activeBullet: '.slider--bullets .active',
allBulletLinks: 'a[href^="#slide-"]',
},
// Object to remove the class attr of 1st child if 1st child isn't active
sliderRemoveFirstClass: function() {
var hash = window.location.hash;
if(hash != "" && hash !== '#slide-1') {
document.querySelector(cssSlider.selectors.firstSlide).removeAttribute('class');
}
},
setClickFrame: function() {
var getContainer = document.getElementsByClassName(cssSlider.selectors.container);
var newNode = document.createElement("a");
var hash = window.location.hash;
var getHashId = 1; // Set initial value to 1 as this the 1st is always loaded
if(hash !== '') {
getHashId = hash.match(/\d+(\.\d+)?/g);
getHashId = parseInt(getHashId[0]);
}
var maxCount = document.querySelectorAll(cssSlider.selectors.sliderItems).length;
var nextItem;
if(getHashId < maxCount) {
nextItem = getHashId+1;
} else {
nextItem = 1;
}
newNode.setAttribute('class', 'slider-clickframe');
newNode.setAttribute('href', '#slide-'+nextItem);
getContainer[0].appendChild(newNode);
},
changeClickFrame: function() {
var getClickFrame = document.querySelector(cssSlider.selectors.clickFrame);
var hash = window.location.hash;
var getHashId = hash.match(/\d+(\.\d+)?/g);
var maxCount = document.querySelectorAll(cssSlider.selectors.sliderItems).length;
var nextItem;
getHashId = parseInt(getHashId[0]);
if(getHashId < maxCount) {
nextItem = getHashId+1;
} else {
nextItem = 1;
}
getClickFrame.setAttribute('href', '#slide-'+nextItem);
},
setActiveItems: function() {
var hash = window.location.hash;
// Set active Slide
var getActiveSlide = document.querySelector(cssSlider.selectors.activeSlide);
if (getActiveSlide !== null) {
getActiveSlide.removeAttribute('class');
}
// Set active Bullet
var activeSlideBullet = document.querySelector(cssSlider.selectors.activeBullet);
var sliderBulletArray = document.querySelectorAll(cssSlider.selectors.allBulletLinks);
// Remove old active Class
activeSlideBullet.removeAttribute('class');
// Set new active Class
document.querySelector('a[href="'+hash+'"]').setAttribute('class', 'active');
},
init: function() {
var hash = window.location.hash;
// Set clickable frame on Slider
window.addEventListener('DOMContentLoaded', cssSlider.setClickFrame);
window.addEventListener('hashchange', cssSlider.changeClickFrame);
// Set active item
window.addEventListener('hashchange', cssSlider.setActiveItems);
if(hash.indexOf("slide-") >= 0) {
window.addEventListener('DOMContentLoaded', cssSlider.setActiveItems);
}
}
};
// Initialize cssSlider
cssSlider.init();
}());