-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmooth.js
107 lines (92 loc) · 3.31 KB
/
smooth.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
/**
* Smooth.js v0.1
* small js library for CSS3 transitions with fallback to $.animate in case of IE
* http://github.com/alevkon/smooth/
*
* Copyright 2011, Alexey Konyshev [email protected]
* MIT license
*/
(function( $ ) {
var Smooth = $.fn.smooth = function(styleMapArgument, settingsArgument) {
var self = this
, styleMap = {}
, promise = $.Deferred()
, settings = $.extend(true, {}, {
duration: 500,
easing: "linear"
}, settingsArgument);
for (var i in styleMapArgument) {
styleMap[i] = styleMapArgument[i];
}
if (Smooth.settings.mode == Smooth.MODE_JQUERY) {
$.extend(true, settings, {
complete: function() {
promise.resolve();
}
});
if (styleMap.transform) {
var transformMap = {};
for (var i=0; i < Smooth.TRANSITION_PREFIXES.length; i++) {
var prefix = Smooth.TRANSITION_PREFIXES[i];
transformMap[prefix + "transform"] = styleMap.transform;
}
this.css(transformMap);
}
this.animate(styleMap, settings);
} else {
//fixing easing CSS3-jQuery difference
if ("swing" == settings.easing) {
settings.easing = "ease";
}
var property = ""
, transitionMap = {};
for (var i in styleMap) {
if (property) {
property += ",";
}
property += i;
}
if (styleMap.transform) {
for (var i=0; i < Smooth.TRANSITION_PREFIXES.length; i++) {
var prefix = Smooth.TRANSITION_PREFIXES[i];
styleMap[prefix + "transform"] = styleMap.transform;
}
}
//todo: cut prefixes depend on browser
for (var i=0; i < Smooth.TRANSITION_PREFIXES.length; i++) {
var prefix = Smooth.TRANSITION_PREFIXES[i];
transitionMap[prefix + "transition-property"] = property;
transitionMap[prefix + "transition-duration"] = (settings.duration / 1000) + " s";
transitionMap[prefix + "transition-timing-function"] = settings.easing;
}
this.css(transitionMap);
this.css(styleMap);
setTimeout(function() {
promise.resolve();
}, settings.duration);
}
promise.done(function() {
if (settingsArgument && settingsArgument.complete && settingsArgument.complete instanceof Function) {
settingsArgument.complete.apply(self, []);
}
});
return promise;
};
$.extend(Smooth, {
MODE_JQUERY: "jquery",
MODE_CSS: "css",
TRANSITION_PREFIXES: ["", "-o-", "-moz-", "-webkit-", "-ms-"],
settings:{
mode: "css"
},
configure: function(settings) {
$.extend(true, this.settings, settings);
},
init: function() {
this.configure({
mode: ($.browser.ie) ? this.MODE_JQUERY : this.MODE_CSS
});
}
});
Smooth.configure();
})( jQuery );