-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider.js
More file actions
170 lines (151 loc) · 5.09 KB
/
slider.js
File metadata and controls
170 lines (151 loc) · 5.09 KB
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
var slider = function (sliderElement) {
var pages = [];
var currentSlide = 1;
var isChanging = false;
var keyUp = { 38: 1, 33: 1 };
var keyDown = { 40: 1, 34: 1 };
var init = function () {
document.body.classList.add('slider__body');
// control scrolling
whatWheel =
'onwheel' in document.createElement('div')
? 'wheel'
: document.onmousewheel !== undefined
? 'mousewheel'
: 'DOMMouseScroll';
window.addEventListener(whatWheel, function (e) {
var direction = e.wheelDelta || e.deltaY;
if (direction > 0) {
changeSlide(-1);
} else {
changeSlide(1);
}
});
// allow keyboard input
window.addEventListener('keydown', function (e) {
if (keyUp[e.keyCode]) {
changeSlide(-1);
} else if (keyDown[e.keyCode]) {
changeSlide(1);
}
});
// page change animation is done
detectChangeEnd() &&
document.querySelector(sliderElement).addEventListener(detectChangeEnd(), function () {
if (isChanging) {
setTimeout(function () {
isChanging = false;
window.location.hash = document.querySelector('[data-slider-index="' + currentSlide + '"]').id;
}, 400);
}
});
// set up page and build visual indicators
document.querySelector(sliderElement).classList.add('slider__container');
var indicatorContainer = document.createElement('div');
indicatorContainer.classList.add('slider__indicators');
var index = 1;
[].forEach.call(document.querySelectorAll(sliderElement + ' > *'), function (section) {
var indicator = document.createElement('a');
indicator.classList.add('slider__indicator');
indicator.setAttribute('data-slider-target-index', index);
indicatorContainer.appendChild(indicator);
section.classList.add('slider__page');
pages.push(section);
section.setAttribute('data-slider-index', index++);
});
document.body.appendChild(indicatorContainer);
document
.querySelector('a[data-slider-target-index = "' + currentSlide + '"]')
.classList.add('slider__indicator--active');
// stuff for touch devices
var touchStartPos = 0;
var touchStopPos = 0;
var touchMinLength = 90;
document.addEventListener('touchstart', function (e) {
e.preventDefault();
if (e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend' || e.type == 'touchcancel') {
var touch = e.touches[0] || e.changedTouches[0];
touchStartPos = touch.pageY;
}
});
document.addEventListener('touchend', function (e) {
e.preventDefault();
if (e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend' || e.type == 'touchcancel') {
var touch = e.touches[0] || e.changedTouches[0];
touchStopPos = touch.pageY;
}
if (touchStartPos + touchMinLength < touchStopPos) {
changeSlide(-1);
} else if (touchStartPos > touchStopPos + touchMinLength) {
changeSlide(1);
}
});
};
// prevent double scrolling
var detectChangeEnd = function () {
var transition;
var e = document.createElement('foobar');
var transitions = {
transition: 'transitionend',
OTransition: 'oTransitionEnd',
MozTransition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd',
};
for (transition in transitions) {
if (e.style[transition] !== undefined) {
return transitions[transition];
}
}
return true;
};
// handle css change
var changeCss = function (obj, styles) {
for (var _style in styles) {
if (obj.style[_style] !== undefined) {
obj.style[_style] = styles[_style];
}
}
};
// handle page/section change
var changeSlide = function (direction) {
// already doing it or last/first page, staph plz
if (isChanging || (direction == 1 && currentSlide == pages.length) || (direction == -1 && currentSlide == 1)) {
return;
}
// change page
currentSlide += direction;
isChanging = true;
changeCss(document.querySelector(sliderElement), {
transform: 'translate3d(0, ' + -(currentSlide - 1) * 100 + '%, 0)',
});
// change dots
document.querySelector('a.slider__indicator--active').classList.remove('slider__indicator--active');
document
.querySelector('a[data-slider-target-index="' + currentSlide + '"]')
.classList.add('slider__indicator--active');
};
// go to spesific slide if it exists
var gotoSlide = function (where) {
var target = document.querySelector(where).getAttribute('data-slider-index');
if (target != currentSlide && document.querySelector(where)) {
changeSlide(target - currentSlide);
}
};
// if page is loaded with hash, go to slide
if (location.hash) {
setTimeout(function () {
window.scrollTo(0, 0);
gotoSlide(location.hash);
}, 1);
}
// we have lift off
if (document.readyState === 'complete') {
init();
} else {
window.addEventListener('onload', init(), false);
}
// expose gotoSlide function
return {
gotoSlide: gotoSlide,
};
};