Last active
August 29, 2015 14:04
-
-
Save chunpu/4d3a836d53410471753d to your computer and use it in GitHub Desktop.
jQuery css3 animate, if browser support css3 transition, use it. (ps: however, the performance is not obvious improved, and it is not in promise queue, so just for simplest situation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function($, doc) { | |
if (!$ || !$.fn) return | |
var css3 = function() { | |
var style = doc.body.style | |
var test = ['transition', 'webkitTransition', 'MozTransition', 'msTransition'] | |
for (var i = 0, x; x = test[i++];) { | |
if (x in style) { | |
return x | |
} | |
} | |
}() | |
if (!css3) return // do nothing | |
$.fn.animate = function(prop, speed, easing, fn) { | |
var self = this | |
var duration = 1000, ease = 'ease', | |
callback = arguments[arguments.length - 1] | |
if (typeof speed == 'number') | |
duration = speed | |
if (typeof easing == 'string') | |
ease = easing | |
if (typeof callback == 'function') { | |
self.one('transitionend', function(ev) { | |
self.css(css3, '') | |
var target = ev.target | |
callback.call(target) | |
}) | |
} else { | |
setTimeout(function() { | |
self.css(css3, '') | |
}, duration) | |
} | |
return self.css(css3, ['all', duration + 'ms', ease].join(' ')).css(prop) | |
} | |
}(window.$, document) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment