-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtakeoff.js
191 lines (160 loc) · 4.82 KB
/
takeoff.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
(function ( $ ) {
$.takeoff = function(userOptions) {
///////////////
/// OPTIONS ///
///////////////
var options = $.extend({}, $.takeoff.defaults, userOptions);
/////////////////////////
/// RUNTIME VARIABLES ///
/////////////////////////
var bgColorArray = [];
var ee = new EventEmitter();
var container = $(".takeoff__backgrounds");
var sectionCount = $(".takeoff__backgroundBlock").length;
var textScrolling = true;
var screenScrolling = true;
//////////////////////
/// INITIALIZATION ///
//////////////////////
var smartscroll = $.smartscroll({
mode: "set",
autoHash: false,
sectionScroll: true,
initialScroll: false,
keepHistory: false,
sectionWrapperSelector: ".takeoff__backgrounds",
sectionSelector: ".takeoff__backgroundBlock",
animationSpeed: 700,
headerHash: "header",
breakpoint: null,
innerSectionScroll: false,
ie8: false,
toptotop: true,
eventEmitter: ee
});
if(options.changingBackground === true) {
// Fill the bgColorArray array with values from
// the data-tf-background-color attribute of each section
$(".takeoff__backgroundBlock").each(function(i, el) {
bgColorArray.push($(el).data("tfBackgroundColor") || "#ffffff");
});
// Sets the background initially
$('body').css({
'backgroundColor': $(".takeoff__backgroundBlock:first").data("tfBackgroundColor")
});
}
///////////////////////
/// EVENT LISTENERS ///
///////////////////////
// When `.takeoff__nextSlide` element is clicked
// scroll down one slide
var handleScrollDown = function () {
smartscroll.scroll(1);
};
$(".takeoff__nextSlide").on('click', handleScrollDown);
// Listen for swipe events
var swipeUpListener = function () {
smartscroll.scroll(1);
}
var swipeDownListener = function () {
smartscroll.scroll(0);
}
ee.addListener('swipeUp', swipeUpListener);
ee.addListener('swipeDown', swipeDownListener);
// Change Background, Text and Screen
var changeBgTextAndScreen = function (screenNumber) {
// Ensure the page is not scrolling at the moment
if(!textScrolling && !screenScrolling) {
textScrolling = screenScrolling = true;
changeBackgroundColor(screenNumber);
changeText(screenNumber);
changeScreen(screenNumber);
}
}
// Change background colour
var changeBackgroundColor = function (slideNumber) {
if(options.changingBackground === true) {
// Uses CSS3 Transitions to fade the background colour
$('body').css({
'backgroundColor': bgColorArray[slideNumber - 1]
});
}
}
// Change mobile screen
var changeScreen = function (screenNumber) {
screenNumber = Math.min(Math.max(screenNumber - 1, 0), $(".takeoff__screens img").length - 1);
var currentScreen = $(".takeoff__screens img.current").eq(0);
var nextScreen = $(".takeoff__screens img").eq(screenNumber);
if (nextScreen.is(currentScreen)) {
screenScrolling = false;
return false;
}
currentScreen.css('z-index', 5);
nextScreen.css('z-index', 6);
nextScreen.animate({
top: 0
}, {
duration: options.slideDuration,
easing: "swing",
complete: function () {
currentScreen.removeClass("current");
nextScreen.addClass("current");
currentScreen.removeAttr('style');
screenScrolling = false;
}
});
}
var changeText = function (screenNumber) {
screenNumber = Math.min(Math.max(screenNumber - 1, 0), $(".takeoff__textblock").length - 1);
var currentText = $(".takeoff__textblock.current").eq(0);
var nextText = $(".takeoff__textblock").eq(screenNumber);
if (currentText.is(nextText)) {
textScrolling = false;
return false;
}
nextText.css('z-index', 5);
currentText.animate({
opacity: 0,
top: "-50px"
}, {
duration: 400,
easing: "swing",
complete: function () {
currentText.removeClass("current");
currentText.removeAttr('style');
}
});
nextText.animate({
top: 0
}, {
duration: options.slideDuration,
easing: "swing",
complete: function () {
nextText.addClass("current");
textScrolling = false;
}
});
}
ee.addListeners('scrollStart', [changeBgTextAndScreen]);
// Animate the first screen and text block
$(".takeoff__screens img").eq(0).add($(".takeoff__textblock").eq(0)).animate({
top: 0
}, {
duration: 400,
easing: "swing",
complete: function () {
$(".takeoff__screens img").eq(0).add($(".takeoff__textblock").eq(0)).addClass("current");
textScrolling = screenScrolling = false;
}
});
// Ensure page is scrolled back to top of page on load
$(document).ready(function() {
$("html,body").animate({scrollTop: 0}, 1000);
});
return container;
}
$.takeoff.defaults = {
changingBackground: true,
slideDuration: 700
}
}( jQuery ));