forked from idibidiart/d3-cssTransition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3-cssTransition.js
67 lines (58 loc) · 2.08 KB
/
d3-cssTransition.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
;(function() {
function simulateTransitionEnd(duration) {
var el = this
, callback = function() {
// don't simulate if naturally triggered
if (!triggered) {
$(el).trigger(vendorEndEvent);
}
}
setTimeout(callback, duration + 17)
}
d3.vendorStyle = function(prop) {
var vendorPrefixes = ['Moz','Webkit','O', 'ms']
, style = document.createElement('div').style
, upper = prop.charAt(0).toUpperCase() + prop.slice(1)
, pref, len = vendorPrefixes.length;
while (len--) {
if ((vendorPrefixes[len] + upper) in style) {
pref = (vendorPrefixes[len])
}
}
if (!pref && prop in style) {
pref = prop
return perf
}
if (pref) {
return pref + upper
}
return ''
}
var endEvent = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd otransitionend',
'msTransition' : 'MSTransitionEnd',
'transition' : 'transitionend'
}
, vendorEndEvent = endEvent[d3.vendorStyle('transition')]
, defaults = {
duration: 400,
easing: 'cubic-bezier(0.250, 0.460, 0.450, 0.940)' /* easeOutQuad */
}
, triggered = false
d3.selection.prototype.cssTransition = function(props, opts) {
var options = $.extend({}, defaults, opts);
var vendorTransition = d3.vendorStyle('transition')
props[vendorTransition] = 'all ' + options.duration + 'ms ' + options.easing;
$(this[0]).one(vendorEndEvent, options.complete || function(){});
$(this[0]).one(vendorEndEvent, function() {
triggered = true
})
simulateTransitionEnd.bind(this[0])(options.duration);
$(this).each(function(i, el) {
$(el).css(props);
})
return this
}
})();