-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.unfold.js
122 lines (104 loc) · 3.09 KB
/
jquery.unfold.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
(function($){
$.fn.unfold = function(opts) {
var $that = $(this);
var ext = navigator.userAgent.match(/Safari/) ? 'webkit' : 'moz';
// Measure element width
var $measure = $that.clone().css({display:'block'});
$that.after($measure);
var width = $measure.width();
$measure.remove();
opts = $.extend({
slices: Math.round($that.height() / 100),
duration: 600,
perspective: 800,
operation: 'open',
easing: null,
collapse: false,
shadow: true
}, opts);
var sliceHeight = $that.height() / opts.slices;
var $div = $('<div>').css({
position: 'relative',
WebkitPerspective: opts.perspective,
MozPerspective: opts.perspective,
WebkitPerspectiveOrigin: 'center 50%',
MozPerspectiveOrigin: 'center 50%'
});
$that.wrap($div);
var $main = $that.parent().empty();
for (var i = 0; i < opts.slices; i++) (function(i) {
var even = (i % 2) == 0;
var $slice = $('<div>').css({
position: 'relative',
height: 0
});
if (ext == 'moz') {
$slice.css({
MozPerspective: opts.perspective, // Fix FF perspective issue
overflow: 'hidden',
});
}
var origin = even ? '0px 0px' : '0px ' + sliceHeight + 'px';
var $outer = $('<div>').css({
overflow: 'hidden',
position: 'absolute',
width: width,
height: sliceHeight,
WebkitTransformOrigin: origin,
MozTransformOrigin: origin
});
if (even) {
$outer.css('top', 0);
}
else {
$outer.css('bottom', 0)
}
var $inner = $('<div>').css({
position: 'absolute',
top: -(sliceHeight * i)
});
var $copy = $that.clone().css({display: 'block'});
opts.collapse && $('> *:first', $copy).css('margin-top', 0);
$inner.append($copy);
$outer.append($inner);
$slice.append($outer);
if (opts.shadow) {
var $shadow = $outer.clone().empty().css({
backgroundImage: '-' + ext + '-linear-gradient(top, black, transparent)',
zIndex: 1
});
$slice.append($shadow);
}
$main.append($slice);
$outer.animate({ foo: 1 }, {
duration: opts.duration,
easing: opts.easing,
step: function(v) {
var degs = 90 - 90 * v;
if (even) degs *= -1;
var rads = Math.abs(90 - degs) * Math.PI / 180;
var h = Math.sin(rads) * (sliceHeight);
var transform = 'rotateX(' + degs + 'deg)';
$outer.css({
WebkitTransform: transform,
MozTransform: transform
});
$outer.parent().css('height', h);
opts.shadow && $shadow.css({
WebkitTransform: transform,
MozTransform: transform,
opacity: .2 - .2 * v
});
},
complete: function() {
$main.replaceWith($that.css('display', 'block'));
}
});
})(i);
return this;
};
$.fn.fold = function(opts) {
opts.operation = 'close';
$(this).unfold(opts);
};
})(jQuery);