forked from NinjaBCN/zepto-slide-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zepto-slide-transition.js
136 lines (123 loc) · 5.01 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Zepto plugin : slide transition v1.1
Modified / fixed version of
https://github.com/NinjaBCN/zepto-slide-transition
*/
/* global Zepto */
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(['zepto'], factory);
} else {
factory(Zepto);
}
}(function($) {
var slide = function(direction) {
return function(duration, callback) {
var callback = $.isFunction(duration) ? duration : $.isFunction(callback) ? callback : undefined,
duration = $.isNumeric(duration) ? duration : undefined;
this.each(function() {
var target = $(this),
position = target.css('position'),
parentStyle;
if (target.height() === 0 && direction === 'up') {
return;
}
// Only for slideDown
// Prepare element and parent element to get right position and css properties
if (direction === 'down') {
parentStyle = target.parent().attr('style');
// show element if it is hidden
target.show();
// place it so it displays as usually but hidden
target.parent().css({
position: 'relative'
});
target.css({
position: 'absolute',
visibility: 'hidden'
});
} // End only for slideDown
var height = target.height(),
marginTop = target.css('margin-top'),
marginBottom = target.css('margin-bottom'),
paddingTop = target.css('padding-top'),
paddingBottom = target.css('padding-bottom');
if (direction === 'down') {
// All slideDown specific actions
target.parent().attr('style', parentStyle);
// set initial css for animation
target.css({
position: position,
visibility: 'visible',
overflow: 'hidden',
height: 0,
marginTop: 0,
marginBottom: 0,
paddingTop: 0,
paddingBottom: 0
});
// animate to gotten height, margin and padding
target.animate({
height: height,
marginTop: marginTop,
marginBottom: marginBottom,
paddingTop: paddingTop,
paddingBottom: paddingBottom
}, {
duration: duration,
complete: function() {
target.attr('style', '');
target.show();
callback && callback.call(target);
}
});
// End all slideDown specific actions
} else {
// All slideUp specific actions
// set initial css for animation
target.css({
display: 'block',
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
}, {
duration: duration,
queue: false,
// callback : restore defaults but hide
complete: function() {
target.attr('style', '');
target.hide();
callback && callback.call(target);
}
});
// End all slideUp specific actions
}
}); // End each
};
};
['up', 'down'].forEach(function(direction) {
var fnName = direction.substr(0, 1).toUpperCase() + direction.substr(1);
$.fn['slide' + fnName] = slide(direction);
});
/* SlideToggle */
$.fn.slideToggle = function(duration, callback) {
if (this.height() === 0) {
// If the element is hidden, slideDown!
this.slideDown(duration, callback);
} else {
// If the element is visible, slideUp!
this.slideUp(duration, callback);
}
};
}));