-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjquery.prezento.js
248 lines (215 loc) · 7.17 KB
/
jquery.prezento.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Name: jQuery Prezento
* Original author: @ivaldi
* Licensed under the GNU Affero General Public License Version 3
*/
;(function($){
"use strict";
$.fn.prezento = function(options){
// Set defaults
var config = $.extend({
devices: [],
debug: false,
deviceHolder: 'prezento-device',
deviceScreen: 'prezento-screen',
startAfterScroll : false,
distanceTop: 0.25,
resetWhenBelow: false,
responsive: 'window',
autoPlay: true
}, options ),
parentElem = this,
deviceName = [],
selectedDevice,
currentPlayState;
// Set value for distanceTop, depends if the user entered a float or string
config.distanceTop = (typeof(config.distanceTop) === 'string') ? parseFloat(config.distanceTop)/100 : config.distanceTop;
// Create layout
parentElem
.css({'position': 'relative', 'display': 'inline-block', 'margin' : '0 auto', 'width' : '100%', 'text-align' : 'center' })
.append('<div class="'+config.deviceHolder+'"></div>')
.append('<div class="'+config.deviceScreen+'"></div>');
// Initialization logic
function init(){
sortDevice();
setBreakpoints(config.devices.length);
}
// Order devices based on breakpoint value, output highest first
function sortDevice(){
config.devices.sort(function(a,b) {
return b.breakpoint - a.breakpoint;
});
}
// Change layout of screen based on selected width
function setBreakpoints(numberOfDevices){
if(config.responsive === 'parent'){
var windowWidth = parentElem.parent().width();
}else{
var windowWidth = $(window).width();
}
for(var i = 0; i < numberOfDevices; i++){
deviceName[config.devices[i].name] = i;
}
for(var i = 0; i < numberOfDevices; i++){
if(windowWidth - config.devices[i].breakpoint > 0){
selectedDevice = i;
setPlaceholderImage(selectedDevice);
break;
}
}
}
// Create animation based on device and tell if it should play, pause, resume or reset
function animAction(selectedDevice, motion){
var anim = 'moveBG ' + config.devices[selectedDevice].bgTransitionDuration + ' linear forwards',
position = calcBackgroundPosition(),
duration = ((100-position) / 100) * config.devices[selectedDevice].bgTransitionDuration.replace("s",""),
transi = 'all ' + duration + 's linear';
if(motion === 'play'){
$('.'+config.deviceScreen).css({
'animation': anim,
'transition': 'none'
});
}
if(motion === 'pause'){
$('.'+config.deviceScreen).css({
'background-position' : '0% ' + position +'%',
'animation': 'none',
'transition': 'none'
});
currentPlayState = 'paused';
}
if(motion === 'resume'){
if(currentPlayState === 'paused'){
$('.'+config.deviceScreen).css({
'background-position' : '0% 100%',
'transition': transi,
});
currentPlayState = 'resumed';
}
}
if(motion === 'reset'){
$('.'+config.deviceScreen).css({
'background-position' : '0% 0%',
'animation': 'none',
'transition': 'none'
});
}
}
// Calculate position of background and return value
function calcBackgroundPosition(){
var myPos = $('.' + config.deviceScreen).css("background-position").split(" ");
myPos = myPos[1].replace("%","");
return myPos;
}
// Detect scrollposition and trigger animation based on distance top
function scrollScreen(){
if(typeof selectedDevice === 'undefined'){
init();
}
var deviceTop = parentElem.offset().top,
deviceHeight = parentElem.height(),
winHeight = $(window).height(),
windowScroll = $(document).scrollTop();
if((deviceTop - windowScroll) / winHeight < config.distanceTop && (deviceTop - windowScroll + deviceHeight) / winHeight > 0){
animAction(selectedDevice, 'play');
}else{
if(config.resetWhenBelow){
animAction(selectedDevice, 'reset');
}
}
}
// Output debug values
function debugDevice(){
if(!window.console){ window.console = {log: function(){} }; }
if (typeof console != "undefined" && typeof console.debug != "undefined") {
console.log(config);
}
}
// Create a dummyImage so we can get original size later
function setPlaceholderImage(selectedDevice){
var dummyImage = new Image();
dummyImage.src = config.devices[selectedDevice].deviceImageSRC;
dummyImage.onload = function(){
initDeviceShowcase(selectedDevice, dummyImage);
}
// Reset values to default
$('.'+config.deviceScreen).css({
'background-position' : '0% 0%',
'animation': 'none',
'transition': 'none'
});
}
// Start calculation based on screensize
function initDeviceShowcase(selectedDevice, dummyImage){
var device = config.devices[selectedDevice],
deviceHolder = '.' + config.deviceHolder,
deviceScreen = '.' + config.deviceScreen,
imageWidth = dummyImage.naturalWidth,
imageHeight = dummyImage.naturalHeight;
$(deviceHolder).html($(dummyImage).clone());
$(deviceHolder).find('img').load(function(){
var deviceWidth = $(this).width(),
deviceHeight = $(this).height(),
screenWidth = device.xRightBottom - device.xLeftTop,
screenHeight = device.yRightBottom - device.yLeftTop,
wRatio = screenWidth / imageWidth,
hRatio = screenHeight / imageHeight,
lRatio = device.xLeftTop / imageWidth,
tRatio = device.yLeftTop / imageHeight;
// Add some basic styling
$(deviceHolder).css({
'position' : 'relative',
'z-index' : 3
});
$(deviceScreen).css({
'position' : 'absolute',
'z-index' : 2,
'width' : Math.ceil(deviceWidth * wRatio),
'height': Math.ceil(deviceHeight * hRatio),
'left' : Math.round(deviceWidth * lRatio),
'top' : Math.round(deviceHeight * tRatio),
'background-size' : '100%',
'background-image' : 'url('+device.bgImgSrc+')',
'background-position' : '0% 0%'
});
});
// Extra options based on config
if(!config.startAfterScroll && config.autoPlay){
animAction(selectedDevice, 'play');
}
}
if(config.devices.length != 0){
// Init plugin
if(config.debug){
$(document).on("ready", debugDevice);
}
if(config.startAfterScroll){
$(window).on("scroll", scrollScreen);
}
if(config.responsive === 'window' || config.responsive === 'parent'){
$(window).on("resize", init);
}
$(window).on("load", init);
}else{
parentElem.append('<p class="pserror">You haven\'t defined any devices. Please read <a href="https://github.com/ivaldi/prezento/blob/master/README.md">the instructions</a> on how to do this. At least one device is needed for this plugin to work.</p>')
}
// Extend plugin with new functions
$.extend($.fn.prezento, {
play: function(){
animAction(selectedDevice, 'play');
},
pause: function(){
animAction(selectedDevice, 'pause');
},
resume: function(){
animAction(selectedDevice, 'resume');
},
reset: function(){
animAction(selectedDevice, 'reset');
},
changeDevice: function(name){
setPlaceholderImage(deviceName[name]);
}
});
}
}(jQuery));