-
Notifications
You must be signed in to change notification settings - Fork 13
/
paperfold.js
287 lines (245 loc) · 7.62 KB
/
paperfold.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*! Paperfold JS
* http://felixniklas.com/paperfoldjs
*
* Copyright (c) 2012-2015 @mrflix
* Available under the MIT license
*/
;(function($){
$.fn.paperfold = function(options){
// support multiple elements
if (this.length > 1){
this.each(function() { $(this).paperfold(options) });
return this;
}
// Default settings
var settings = $.extend({
duration: 240,
maxFoldHeight: 200,
folds: null,
perspective: '1000px',
topShadow: 'linear-gradient(transparent, rgba(0,0,0,.5))',
bottomShadow: 'linear-gradient(rgba(0,0,0,.5), transparent)',
isOpen: false,
onProgress: $.noop,
stayInPlace: false
}, options);
var css = {
holder: {
height: '',
position: 'relative',
overflow: 'hidden'
},
fold: {
height: 0,
position: 'relative',
transformStyle: 'preserve-3d',
perspective: settings.perspective,
willChange: 'height'
},
side: {
overflow: 'hidden',
width: '100%',
willChange: 'transform'
},
top: {
transformOrigin: 'top',
transform: 'rotateX(-90deg)',
position: 'relative'
},
bottom: {
transformOrigin: 'bottom',
transform: 'rotateX(90deg)',
position: 'absolute',
bottom: 0
},
inner: {
position: 'absolute',
width: '100%'
},
shadow: {
position: 'absolute',
width: '100%',
height: '100%',
top: 0,
left: 0,
willChange: 'opacity'
},
topShadow: {
background: settings.topShadow
},
bottomShadow: {
background: settings.bottomShadow
}
}
// extend top and bottom with side class
css.top = $.extend(css.top, css.side);
css.bottom = $.extend(css.bottom, css.side);
// extend shadows with shadow class
css.topShadow = $.extend(css.topShadow, css.shadow);
css.bottomShadow = $.extend(css.bottomShadow, css.shadow);
/* rAF shim. Gist: https://gist.github.com/julianshapiro/9497513 */
var rAFShim = (function() {
var timeLast = 0;
return window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) {
var timeCurrent = (new Date()).getTime(),
timeDelta;
/* Dynamically set delay on a per-tick basis to match 60fps. */
/* Technique by Erik Moller. MIT license: https://gist.github.com/paulirish/1579671 */
timeDelta = Math.max(0, 16 - (timeCurrent - timeLast));
timeLast = timeCurrent + timeDelta;
return setTimeout(function() { callback(timeCurrent + timeDelta); }, timeDelta);
};
})();
function getScrollParent(element) {
var position = element.css( "position" ),
excludeStaticParent = position === "absolute",
scrollParent = element.parents().filter( function() {
var parent = $( element );
if ( excludeStaticParent && parent.css( "position" ) === "static" ) {
return false;
}
return (/(auto|scroll)/).test( parent.css( "overflow" ) + parent.css( "overflow-y" ) + parent.css( "overflow-x" ) );
}).eq( 0),
parentDocument
;
if(!scrollParent.length) {
if(typeof element[0] !== 'undefined') {
parentDocument = element[0].ownerDocument || document;
} else {
parentDocument = document;
}
} else {
parentDocument = scrollParent;
}
return position === "fixed" || parentDocument;
}
var $this = $(this);
var currentPercentage;
var targetPercentage;
var ticker = window.requestAnimationFrame || rAFShim;
var startTime;
var scrollOffset;
var scrollParent = getScrollParent($this);
var height;
var content;
var $folds = $();
var $bottoms = $();
var $tops = $();
var $shadows = $();
var paperfold = this;
this.initialize = function(){
// get real height
height = $this.show().height();
$this.css(css.holder);
// calculate amount and height of the folds
$this.data('foldCount', settings.folds || Math.ceil(height / settings.maxFoldHeight));
$this.data('foldHeight', Math.floor(height / $this.data('foldCount')));
// clone content
content = $this.children().clone();
// add folds containing the previously cached content
for(var i=0, j=0; i<$this.data('foldCount'); i++, j+=2){
var topHeight = bottomHeight = $this.data('foldHeight')/2;
var fold;
if( (i+1) === $this.data('foldCount') && $this.data('foldHeight')/2 % 2 ){
bottomHeight = height-(j+1)*topHeight;
}
fold = createFold(j, topHeight, bottomHeight);
$folds = $folds.add(fold);
$this.append(fold);
}
if(settings.isOpen){
showContent();
currentPercentage = 1;
} else {
hideContent();
currentPercentage = 0;
}
return this;
}
/*
every fold is holding two identical divs: top and bottom
the children divs are positioned relative to it
the slice grows from zero to 1/n of the elements height (n = number of slices)
*/
function createFold(j, topHeight, bottomHeight){
var offsetTop = -j*topHeight;
var offsetBottom = -height+j*topHeight+$this.data('foldHeight');
var topShadow = $('<div>').css(css.topShadow);
var bottomShadow = $('<div>').css(css.bottomShadow);
var top = $('<div>').css(css.top).css('height', topHeight).append(
$('<div>').css(css.inner).css('top', offsetTop).append(content.clone()).add(topShadow)
);
var bottom = $('<div>').css(css.bottom).css('height', bottomHeight).append(
$('<div>').css(css.inner).css('bottom', offsetBottom).append(content.clone()).add(bottomShadow)
);
$tops = $tops.add(top);
$bottoms = $bottoms.add(bottom);
$shadows = $shadows.add(topShadow.add(bottomShadow));
return $('<div>').css(css.fold).append(top.add(bottom));
}
function drawAt(percentage){
// change angle of tops and bottoms
var c = $this.data('foldHeight') * percentage;
var a = b = $this.data('foldHeight')/2;
var part = 2*b*c;
var bottomAngle = part <= 0 ? 90 : Math.acos( (b*b+c*c-a*a) / part )*180/Math.PI;
var topAngle = 360-bottomAngle;
$tops.css('transform', 'rotateX(' + topAngle + 'deg)');
$bottoms.css('transform', 'rotateX(' + bottomAngle + 'deg)');
// change folds height
var foldHeight = Math.floor(height/$this.data('foldCount') * percentage);
$folds.height(foldHeight);
// adjust the shadows
$shadows.css('opacity', 1 - percentage);
}
function tick(timestamp){
var currentTime = (new Date).getTime();
currentPercentage = Math.min(1, (currentTime - startTime)/settings.duration);
if(targetPercentage === 0)
currentPercentage = 1 - currentPercentage;
drawAt(currentPercentage);
// callback
settings.onProgress(currentPercentage);
// adjust scroll
if(settings.stayInPlace)
scrollParent.scrollTop(scrollOffset + currentPercentage * height);
if(currentPercentage !== targetPercentage){
ticker(tick);
} else {
if(targetPercentage === 1)
showContent();
}
}
function showContent(){
$folds.hide();
$this.children().not($folds).show();
}
function hideContent(){
$folds.show();
$this.children().not($folds).hide();
}
function animateTo(percentage){
startTime = (new Date).getTime();
targetPercentage = percentage;
if(settings.stayInPlace)
scrollOffset = scrollParent.scrollTop();
ticker(tick);
}
this.percentage = drawAt;
this.open = function(){
animateTo(1);
}
this.close = function(){
hideContent();
animateTo(0);
}
this.toggle = function(){
if(currentPercentage < 0.5){
paperfold.open();
} else {
paperfold.close();
}
}
return this.initialize();
}
})(jQuery);