forked from NinjaBCN/zepto-slide-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzepto-slide-transition.js
119 lines (101 loc) · 3.53 KB
/
zepto-slide-transition.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
/* Zepto plugin : slide transition v1.1 */
(function ($) {
/* SlideDown */
$.fn.slideDown = function (duration) {
// get the element position to restore it then
var position = this.css('position');
// show element if it is hidden
this.show();
// place it so it displays as usually but hidden
this.css({
position: 'absolute',
visibility: 'hidden'
});
// get naturally height, margin, padding
var marginTop = this.css('margin-top');
var marginBottom = this.css('margin-bottom');
var paddingTop = this.css('padding-top');
var paddingBottom = this.css('padding-bottom');
var height = this.css('height');
// set initial css for animation
this.css({
position: position,
visibility: 'visible',
overflow: 'hidden',
height: 0,
marginTop: 0,
marginBottom: 0,
paddingTop: 0,
paddingBottom: 0
});
// animate to gotten height, margin and padding
this.animate({
height: height,
marginTop: marginTop,
marginBottom: marginBottom,
paddingTop: paddingTop,
paddingBottom: paddingBottom
}, duration);
};
/* SlideUp */
$.fn.slideUp = function (duration) {
// active the function only if the element is visible
if (this.height() > 0) {
var target = this;
// get the element position to restore it then
var position = target.css('position');
// get the element height, margin and padding to restore them then
var height = target.css('height');
var marginTop = target.css('margin-top');
var marginBottom = target.css('margin-bottom');
var paddingTop = target.css('padding-top');
var paddingBottom = target.css('padding-bottom');
// set initial css for animation
this.css({
visibility: 'visible',
overflow: 'hidden',
height: height,
marginTop: marginTop,
marginBottom: marginBottom,
paddingTop: paddingTop,
paddingBottom: paddingBottom
});
// animate element height, margin and padding to zero
target.animate({
height: 0,
marginTop: 0,
marginBottom: 0,
paddingTop: 0,
paddingBottom: 0
},
{
// callback : restore the element position, height, margin and padding to original values
duration: duration,
queue: false,
complete: function(){
target.hide();
target.css({
visibility: 'visible',
overflow: 'hidden',
height: height,
marginTop: marginTop,
marginBottom: marginBottom,
paddingTop: paddingTop,
paddingBottom: paddingBottom
});
}
});
}
};
/* SlideToggle */
$.fn.slideToggle = function (duration) {
// if the element is hidden, slideDown !
if (this.height() == 0) {
this.slideDown(duration);
}
// if the element is visible, slideUp !
else {
this.slideUp(duration);
}
};
})(Zepto);