forked from santervo/transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transition.js
98 lines (88 loc) · 2.82 KB
/
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
var Transition = function(element, property, transform, duration, delay, easing) {
var self = this;
var elem = $(element);
var start;
var remainingDelay = delay != undefined ? delay : 0;
var remainingDuration = duration;
var paused = true;
// Borrowed from jquery.transit.js
var div = document.createElement('div');
console.log(div.style);
function getVendorPropertyName(prop) {
var prefixes = ['Moz', 'Webkit', 'O', 'ms'];
var prop_ = prop.charAt(0).toUpperCase() + prop.substr(1);
if (prop in div.style) { return prop; }
for (var i=0; i<prefixes.length; ++i) {
var vendorProp = prefixes[i] + prop_;
if (vendorProp in div.style) { return vendorProp; }
}
}
function getVendorCssPropertyName(prop) {
var prefixes = ['Moz', 'Webkit', 'O', 'ms'];
var prop_ = prop.charAt(0).toUpperCase() + prop.substr(1);
if (prop in div.style) { return prop; }
for (var i=0; i<prefixes.length; ++i) {
var vendorProp = prefixes[i] + prop_;
if (vendorProp in div.style) { return "-" + prefixes[i].toLowerCase() + "-" + prop; }
}
}
var vendorProperty = getVendorPropertyName(property);
var vendorCssProperty = getVendorCssPropertyName(property);
var transitionProperty = getVendorPropertyName('transitionProperty');
var transitionDelay = getVendorPropertyName('transitionDelay');
var transitionDuration = getVendorPropertyName('transitionDuration');
var transitionEasing = getVendorPropertyName('transitionTimingFunction');
self.pause = function() {
if (!paused) {
self.countRemaining();
self.unsetTransform();
paused = true;
}
};
self.resume = function() {
if (paused && remainingDuration > 0) {
start = new Date;
self.setTransform();
paused = false;
}
};
self.toggle = function() {
if (paused) {
self.resume();
}
else {
self.pause();
}
};
self.setTransform = function() {
elem.css(vendorProperty, transform);
elem.css(transitionProperty, vendorCssProperty);
if(easing != undefined)
elem.css(transitionEasing, easing);
if (remainingDelay > 0)
elem.css(transitionDelay, remainingDelay + 'ms');
if (remainingDuration > 0)
elem.css(transitionDuration, remainingDuration + 'ms');
};
self.unsetTransform = function() {
var transform = window.getComputedStyle(elem[0])[vendorProperty];
elem.css(vendorProperty, transform);
elem.css(transitionDelay, '0ms');
elem.css(transitionDuration, '0ms');
};
self.countRemaining = function() {
var duration = new Date - start;
if (remainingDelay > 0) {
remainingDelay -= duration;
duration = 0;
}
if (remainingDelay < 0) {
duration = -remainingDelay;
remainingDelay = 0;
}
if (duration > 0) {
remainingDuration -= duration;
}
};
self.resume();
};