forked from ccoenraets/PageSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageslider.js
60 lines (46 loc) · 1.93 KB
/
pageslider.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
/* Notes:
* - History management is currently done using window.location.hash. This could easily be changed to use Push State instead.
* - jQuery dependency for now. This could also be easily removed.
*/
function PageSlider(container) {
var container = container,
currentPage,
stateHistory = [];
// Use this function if you want PageSlider to automatically determine the sliding direction based on the state history
this.slidePage = function(page) {
var l = stateHistory.length,
state = window.location.hash;
if (l === 0) {
stateHistory.push(state);
this.slidePageFrom(page);
return;
}
if (state === stateHistory[l-2]) {
stateHistory.pop();
this.slidePageFrom(page, 'left');
} else {
stateHistory.push(state);
this.slidePageFrom(page, 'right');
}
}
// Use this function directly if you want to control the sliding direction outside PageSlider
this.slidePageFrom = function(page, from) {
container.append(page);
if (!currentPage || !from) {
page.attr("class", "page center");
currentPage = page;
return;
}
// Position the page at the starting position of the animation
page.attr("class", "page " + from);
currentPage.one('webkitTransitionEnd', function(e) {
$(e.target).remove();
});
// Force reflow. More information here: http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/
container[0].offsetWidth;
// Position the new page and the current page at the ending position of their animation with a transition class indicating the duration of the animation
page.attr("class", "page transition center");
currentPage.attr("class", "page transition " + (from === "left" ? "right" : "left"));
currentPage = page;
}
}